+def GetVariableOffset(mapfilepath, efifilepath, varnames):\r
+ """ Parse map file to get variable offset in current EFI file \r
+ @param mapfilepath Map file absolution path\r
+ @param efifilepath: EFI binary file full path\r
+ @param varnames iteratable container whose elements are variable names to be searched\r
+ \r
+ @return List whos elements are tuple with variable name and raw offset\r
+ """\r
+ lines = []\r
+ try:\r
+ f = open(mapfilepath, 'r')\r
+ lines = f.readlines()\r
+ f.close()\r
+ except:\r
+ return None\r
+ \r
+ if len(lines) == 0: return None\r
+ firstline = lines[0].strip()\r
+ if (firstline.startswith("Archive member included ") and\r
+ firstline.endswith(" file (symbol)")):\r
+ return _parseForGCC(lines, efifilepath, varnames)\r
+ if firstline.startswith("# Path:"):\r
+ return _parseForXcode(lines, efifilepath, varnames)\r
+ return _parseGeneral(lines, efifilepath, varnames)\r
+\r
+def _parseForXcode(lines, efifilepath, varnames):\r
+ status = 0\r
+ ret = []\r
+ for index, line in enumerate(lines):\r
+ line = line.strip()\r
+ if status == 0 and line == "# Symbols:":\r
+ status = 1\r
+ continue\r
+ if status == 1 and len(line) != 0:\r
+ for varname in varnames:\r
+ if varname in line:\r
+ m = re.match('^([\da-fA-FxX]+)([\s\S]*)([_]*%s)$' % varname, line)\r
+ if m != None:\r
+ ret.append((varname, m.group(1)))\r
+ return ret\r
+\r
+def _parseForGCC(lines, efifilepath, varnames):\r
+ """ Parse map file generated by GCC linker """\r
+ status = 0\r
+ sections = []\r
+ varoffset = []\r
+ for index, line in enumerate(lines):\r
+ line = line.strip()\r
+ # status machine transection\r
+ if status == 0 and line == "Memory Configuration":\r
+ status = 1\r
+ continue\r
+ elif status == 1 and line == 'Linker script and memory map':\r
+ status = 2\r
+ continue\r
+ elif status ==2 and line == 'START GROUP':\r
+ status = 3\r
+ continue\r
+\r
+ # status handler\r
+ if status == 3:\r
+ m = re.match('^([\w_\.]+) +([\da-fA-Fx]+) +([\da-fA-Fx]+)$', line)\r
+ if m != None:\r
+ sections.append(m.groups(0))\r
+ for varname in varnames:\r
+ Str = ''\r
+ m = re.match("^.data.(%s)" % varname, line)\r
+ if m != None:\r
+ m = re.match(".data.(%s)$" % varname, line)\r
+ if m != None:\r
+ Str = lines[index + 1]\r
+ else:\r
+ Str = line[len(".data.%s" % varname):]\r
+ if Str:\r
+ m = re.match('^([\da-fA-Fx]+) +([\da-fA-Fx]+)', Str.strip())\r
+ if m != None:\r
+ varoffset.append((varname, int(m.groups(0)[0], 16) , int(sections[-1][1], 16), sections[-1][0]))\r
+\r
+ if not varoffset:\r
+ return []\r
+ # get section information from efi file\r
+ efisecs = PeImageClass(efifilepath).SectionHeaderList\r
+ if efisecs == None or len(efisecs) == 0:\r
+ return []\r
+ #redirection\r
+ redirection = 0\r
+ for efisec in efisecs:\r
+ for section in sections:\r
+ if section[0].strip() == efisec[0].strip() and section[0].strip() == '.text':\r
+ redirection = int(section[1], 16) - efisec[1]\r
+\r
+ ret = []\r
+ for var in varoffset:\r
+ for efisec in efisecs:\r
+ if var[1] >= efisec[1] and var[1] < efisec[1]+efisec[3]:\r
+ ret.append((var[0], hex(efisec[2] + var[1] - efisec[1] - redirection)))\r
+ return ret\r
+\r
+def _parseGeneral(lines, efifilepath, varnames):\r
+ status = 0 #0 - beginning of file; 1 - PE section definition; 2 - symbol table\r
+ secs = [] # key = section name\r
+ varoffset = []\r
+ secRe = re.compile('^([\da-fA-F]+):([\da-fA-F]+) +([\da-fA-F]+)[Hh]? +([.\w\$]+) +(\w+)', re.UNICODE)\r
+ symRe = re.compile('^([\da-fA-F]+):([\da-fA-F]+) +([\.:\\\\\w\?@\$]+) +([\da-fA-F]+)', re.UNICODE)\r
+\r
+ for line in lines:\r
+ line = line.strip()\r
+ if re.match("^Start[' ']+Length[' ']+Name[' ']+Class", line):\r
+ status = 1\r
+ continue\r
+ if re.match("^Address[' ']+Publics by Value[' ']+Rva\+Base", line):\r
+ status = 2\r
+ continue\r
+ if re.match("^entry point at", line):\r
+ status = 3\r
+ continue \r
+ if status == 1 and len(line) != 0:\r
+ m = secRe.match(line)\r
+ assert m != None, "Fail to parse the section in map file , line is %s" % line\r
+ sec_no, sec_start, sec_length, sec_name, sec_class = m.groups(0)\r
+ secs.append([int(sec_no, 16), int(sec_start, 16), int(sec_length, 16), sec_name, sec_class])\r
+ if status == 2 and len(line) != 0:\r
+ for varname in varnames:\r
+ m = symRe.match(line)\r
+ assert m != None, "Fail to parse the symbol in map file, line is %s" % line\r
+ sec_no, sym_offset, sym_name, vir_addr = m.groups(0)\r
+ sec_no = int(sec_no, 16)\r
+ sym_offset = int(sym_offset, 16)\r
+ vir_addr = int(vir_addr, 16)\r
+ m2 = re.match('^[_]*(%s)' % varname, sym_name)\r
+ if m2 != None:\r
+ # fond a binary pcd entry in map file\r
+ for sec in secs:\r
+ if sec[0] == sec_no and (sym_offset >= sec[1] and sym_offset < sec[1] + sec[2]):\r
+ varoffset.append([varname, sec[3], sym_offset, vir_addr, sec_no])\r
+\r
+ if not varoffset: return []\r
+\r
+ # get section information from efi file\r
+ efisecs = PeImageClass(efifilepath).SectionHeaderList\r
+ if efisecs == None or len(efisecs) == 0:\r
+ return []\r
+\r
+ ret = []\r
+ for var in varoffset:\r
+ index = 0\r
+ for efisec in efisecs:\r
+ index = index + 1\r
+ if var[1].strip() == efisec[0].strip():\r
+ ret.append((var[0], hex(efisec[2] + var[2])))\r
+ elif var[4] == index:\r
+ ret.append((var[0], hex(efisec[2] + var[2])))\r
+\r
+ return ret\r
+\r
+## Routine to process duplicated INF\r
+#\r
+# This function is called by following two cases:\r
+# Case 1 in DSC:\r
+# [components.arch]\r
+# Pkg/module/module.inf\r
+# Pkg/module/module.inf {\r
+# <Defines>\r
+# FILE_GUID = 0D1B936F-68F3-4589-AFCC-FB8B7AEBC836\r
+# }\r
+# Case 2 in FDF:\r
+# INF Pkg/module/module.inf\r
+# INF FILE_GUID = 0D1B936F-68F3-4589-AFCC-FB8B7AEBC836 Pkg/module/module.inf\r
+#\r
+# This function copies Pkg/module/module.inf to\r
+# Conf/.cache/0D1B936F-68F3-4589-AFCC-FB8B7AEBC836module.inf\r
+#\r
+# @param Path Original PathClass object\r
+# @param BaseName New file base name\r
+#\r
+# @retval return the new PathClass object\r
+#\r
+def ProcessDuplicatedInf(Path, BaseName, Workspace):\r
+ Filename = os.path.split(Path.File)[1]\r
+ if '.' in Filename:\r
+ Filename = BaseName + Path.BaseName + Filename[Filename.rfind('.'):]\r
+ else:\r
+ Filename = BaseName + Path.BaseName\r
+\r
+ #\r
+ # If -N is specified on command line, cache is disabled\r
+ # The directory has to be created\r
+ #\r
+ DbDir = os.path.split(GlobalData.gDatabasePath)[0]\r
+ if not os.path.exists(DbDir):\r
+ os.makedirs(DbDir)\r
+ #\r
+ # A temporary INF is copied to database path which must have write permission\r
+ # The temporary will be removed at the end of build\r
+ # In case of name conflict, the file name is \r
+ # FILE_GUIDBaseName (0D1B936F-68F3-4589-AFCC-FB8B7AEBC836module.inf)\r
+ #\r
+ TempFullPath = os.path.join(DbDir,\r
+ Filename)\r
+ RtPath = PathClass(Path.File, Workspace)\r
+ #\r
+ # Modify the full path to temporary path, keep other unchanged\r
+ #\r
+ # To build same module more than once, the module path with FILE_GUID overridden has\r
+ # the file name FILE_GUIDmodule.inf, but the relative path (self.MetaFile.File) is the real path\r
+ # in DSC which is used as relative path by C files and other files in INF. \r
+ # A trick was used: all module paths are PathClass instances, after the initialization\r
+ # of PathClass, the PathClass.Path is overridden by the temporary INF path.\r
+ #\r
+ # The reason for creating a temporary INF is:\r
+ # Platform.Modules which is the base to create ModuleAutoGen objects is a dictionary,\r
+ # the key is the full path of INF, the value is an object to save overridden library instances, PCDs.\r
+ # A different key for the same module is needed to create different output directory,\r
+ # retrieve overridden PCDs, library instances.\r
+ #\r
+ # The BaseName is the FILE_GUID which is also the output directory name.\r
+ #\r
+ #\r
+ RtPath.Path = TempFullPath\r
+ RtPath.BaseName = BaseName\r
+ #\r
+ # If file exists, compare contents\r
+ #\r
+ if os.path.exists(TempFullPath):\r
+ with open(str(Path), 'rb') as f1: Src = f1.read()\r
+ with open(TempFullPath, 'rb') as f2: Dst = f2.read()\r
+ if Src == Dst:\r
+ return RtPath\r
+ GlobalData.gTempInfs.append(TempFullPath)\r
+ shutil.copy2(str(Path), TempFullPath)\r
+ return RtPath\r
+\r
+## Remove temporary created INFs whose paths were saved in gTempInfs\r
+#\r
+def ClearDuplicatedInf():\r
+ for File in GlobalData.gTempInfs:\r
+ if os.path.exists(File):\r
+ os.remove(File)\r
+\r