]> git.proxmox.com Git - mirror_edk2.git/blobdiff - BaseTools/Source/C/GenFw/ElfConvert.c
BaseTool: Add cache for the result of SkipAutogen.
[mirror_edk2.git] / BaseTools / Source / C / GenFw / ElfConvert.c
index 1a84d3c28794f702036d4365b6e7a53e57f1e3a0..494f9b8e90cfe0e64474987360d50b634471d2d5 100644 (file)
@@ -1,11 +1,11 @@
 /** @file\r
 Elf convert solution\r
 \r
-Copyright (c) 2010 - 2014, Intel Corporation. All rights reserved.<BR>\r
+Copyright (c) 2010 - 2018, Intel Corporation. All rights reserved.<BR>\r
 \r
-This program and the accompanying materials are licensed and made available \r
-under the terms and conditions of the BSD License which accompanies this \r
-distribution.  The full text of the license may be found at \r
+This program and the accompanying materials are licensed and made available\r
+under the terms and conditions of the BSD License which accompanies this\r
+distribution.  The full text of the license may be found at\r
 http://opensource.org/licenses/bsd-license.php\r
 \r
 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
@@ -24,6 +24,7 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
 #include <string.h>\r
 #include <time.h>\r
 #include <ctype.h>\r
+#include <assert.h>\r
 \r
 #include <Common/UefiBaseTypes.h>\r
 #include <IndustryStandard/PeImage.h>\r
@@ -56,6 +57,11 @@ UINT32 mCoffOffset;
 //\r
 UINT32 mTableOffset;\r
 \r
+//\r
+//mFileBufferSize\r
+//\r
+UINT32 mFileBufferSize;\r
+\r
 //\r
 //*****************************************************************************\r
 // Common ELF Functions\r
@@ -86,7 +92,7 @@ CoffAddFixup(
       // Add a null entry (is it required ?)\r
       //\r
       CoffAddFixupEntry (0);\r
-      \r
+\r
       //\r
       // Pad for alignment.\r
       //\r
@@ -96,11 +102,15 @@ CoffAddFixup(
 \r
     mCoffFile = realloc (\r
       mCoffFile,\r
-      mCoffOffset + sizeof(EFI_IMAGE_BASE_RELOCATION) + 2*0x1000\r
+      mCoffOffset + sizeof(EFI_IMAGE_BASE_RELOCATION) + 2 * MAX_COFF_ALIGNMENT\r
       );\r
+    if (mCoffFile == NULL) {\r
+      Error (NULL, 0, 4001, "Resource", "memory cannot be allocated!");\r
+    }\r
+    assert (mCoffFile != NULL);\r
     memset (\r
       mCoffFile + mCoffOffset, 0,\r
-      sizeof(EFI_IMAGE_BASE_RELOCATION) + 2*0x1000\r
+      sizeof(EFI_IMAGE_BASE_RELOCATION) + 2 * MAX_COFF_ALIGNMENT\r
       );\r
 \r
     mCoffBaseRel = (EFI_IMAGE_BASE_RELOCATION*)(mCoffFile + mCoffOffset);\r
@@ -153,7 +163,7 @@ IsElfHeader (
   UINT8  *FileBuffer\r
 )\r
 {\r
-  return (FileBuffer[EI_MAG0] == ELFMAG0 && \r
+  return (FileBuffer[EI_MAG0] == ELFMAG0 &&\r
           FileBuffer[EI_MAG1] == ELFMAG1 &&\r
           FileBuffer[EI_MAG2] == ELFMAG2 &&\r
           FileBuffer[EI_MAG3] == ELFMAG3);\r
@@ -168,6 +178,7 @@ ConvertElf (
   ELF_FUNCTION_TABLE              ElfFunctions;\r
   UINT8                           EiClass;\r
 \r
+  mFileBufferSize = *FileLength;\r
   //\r
   // Determine ELF type and set function table pointer correctly.\r
   //\r
@@ -188,7 +199,7 @@ ConvertElf (
 \r
   //\r
   // Compute sections new address.\r
-  //  \r
+  //\r
   VerboseMsg ("Compute sections new address.");\r
   ElfFunctions.ScanSections ();\r
 \r
@@ -196,9 +207,15 @@ ConvertElf (
   // Write and relocate sections.\r
   //\r
   VerboseMsg ("Write and relocate sections.");\r
-  ElfFunctions.WriteSections (SECTION_TEXT);\r
-  ElfFunctions.WriteSections (SECTION_DATA);\r
-  ElfFunctions.WriteSections (SECTION_HII);\r
+  if (!ElfFunctions.WriteSections (SECTION_TEXT)) {\r
+    return FALSE;\r
+  }\r
+  if (!ElfFunctions.WriteSections (SECTION_DATA)) {\r
+    return FALSE;\r
+  }\r
+  if (!ElfFunctions.WriteSections (SECTION_HII)) {\r
+    return FALSE;\r
+  }\r
 \r
   //\r
   // Translate and write relocations.\r
@@ -229,6 +246,6 @@ ConvertElf (
   // Free resources used by ELF functions.\r
   //\r
   ElfFunctions.CleanUp ();\r
-  \r
+\r
   return TRUE;\r
 }\r