]> git.proxmox.com Git - mirror_edk2.git/blobdiff - BaseTools/Source/C/GenFw/ElfConvert.c
BaseTools: Fix Section header size larger than elf file size bug
[mirror_edk2.git] / BaseTools / Source / C / GenFw / ElfConvert.c
index 6211389ba1695458ec9e82faee470e1108910b68..86f844d2a9264ca3b82537c3734500cc60883d7a 100644 (file)
@@ -1,7 +1,7 @@
 /** @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
@@ -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
@@ -98,6 +104,10 @@ CoffAddFixup(
       mCoffFile,\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 * MAX_COFF_ALIGNMENT\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
@@ -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