]> git.proxmox.com Git - mirror_edk2.git/blobdiff - BaseTools/Source/C/GenFw/ElfConvert.c
UefiCpuPkg: Move AsmRelocateApLoopStart from Mpfuncs.nasm to AmdSev.nasm
[mirror_edk2.git] / BaseTools / Source / C / GenFw / ElfConvert.c
index 135fa90fa03b2a27da80e93aa15818824e9d36c0..be98544056ec49416c844d21f266c5868f4cea84 100644 (file)
-/** @file
-
-Copyright (c) 2010, Intel Corporation. All rights reserved.<BR>
-
-This program and the accompanying materials are licensed and made available 
-under the terms and conditions of the BSD License which accompanies this 
-distribution.  The full text of the license may be found at 
-http://opensource.org/licenses/bsd-license.php
-
-THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
-WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
-
-**/
-
-#include "WinNtInclude.h"
-
-#ifndef __GNUC__
-#include <windows.h>
-#include <io.h>
-#endif
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <time.h>
-#include <ctype.h>
-
-#include <Common/UefiBaseTypes.h>
-#include <IndustryStandard/PeImage.h>
-
-#include "EfiUtilityMsgs.h"
-
-#include "GenFw.h"
-#include "ElfConvert.h"
-#include "Elf32Convert.h"
-#include "Elf64Convert.h"
-
-//
-// Result Coff file in memory.
-//
-UINT8 *mCoffFile = NULL;
-
-//
-// COFF relocation data
-//
-EFI_IMAGE_BASE_RELOCATION *mCoffBaseRel;
-UINT16                    *mCoffEntryRel;
-
-//
-// Current offset in coff file.
-//
-UINT32 mCoffOffset;
-
-//
-// Offset in Coff file of headers and sections.
-//
-UINT32 mTableOffset;
-
-//
-//*****************************************************************************
-// Common ELF Functions
-//*****************************************************************************
-//
-
-VOID
-CoffAddFixupEntry(
-  UINT16 Val
-  )
-{
-  *mCoffEntryRel = Val;
-  mCoffEntryRel++;
-  mCoffBaseRel->SizeOfBlock += 2;
-  mCoffOffset += 2;
-}
-
-VOID
-CoffAddFixup(
-  UINT32 Offset,
-  UINT8  Type
-  )
-{
-  if (mCoffBaseRel == NULL
-      || mCoffBaseRel->VirtualAddress != (Offset & ~0xfff)) {
-    if (mCoffBaseRel != NULL) {
-      //
-      // Add a null entry (is it required ?)
-      //
-      CoffAddFixupEntry (0);
-      
-      //
-      // Pad for alignment.
-      //
-      if (mCoffOffset % 4 != 0)
-        CoffAddFixupEntry (0);
-    }
-
-    mCoffFile = realloc (
-      mCoffFile,
-      mCoffOffset + sizeof(EFI_IMAGE_BASE_RELOCATION) + 2*0x1000
-      );
-    memset (
-      mCoffFile + mCoffOffset, 0,
-      sizeof(EFI_IMAGE_BASE_RELOCATION) + 2*0x1000
-      );
-
-    mCoffBaseRel = (EFI_IMAGE_BASE_RELOCATION*)(mCoffFile + mCoffOffset);
-    mCoffBaseRel->VirtualAddress = Offset & ~0xfff;
-    mCoffBaseRel->SizeOfBlock = sizeof(EFI_IMAGE_BASE_RELOCATION);
-
-    mCoffEntryRel = (UINT16 *)(mCoffBaseRel + 1);
-    mCoffOffset += sizeof(EFI_IMAGE_BASE_RELOCATION);
-  }
-
-  //
-  // Fill the entry.
-  //
-  CoffAddFixupEntry((UINT16) ((Type << 12) | (Offset & 0xfff)));
-}
-
-VOID
-CreateSectionHeader (
-  const CHAR8 *Name,
-  UINT32      Offset,
-  UINT32      Size,
-  UINT32      Flags
-  )
-{
-  EFI_IMAGE_SECTION_HEADER *Hdr;
-  Hdr = (EFI_IMAGE_SECTION_HEADER*)(mCoffFile + mTableOffset);
-
-  strcpy((char *)Hdr->Name, Name);
-  Hdr->Misc.VirtualSize = Size;
-  Hdr->VirtualAddress = Offset;
-  Hdr->SizeOfRawData = Size;
-  Hdr->PointerToRawData = Offset;
-  Hdr->PointerToRelocations = 0;
-  Hdr->PointerToLinenumbers = 0;
-  Hdr->NumberOfRelocations = 0;
-  Hdr->NumberOfLinenumbers = 0;
-  Hdr->Characteristics = Flags;
-
-  mTableOffset += sizeof (EFI_IMAGE_SECTION_HEADER);
-}
-
-//
-//*****************************************************************************
-// Functions called from GenFw main code.
-//*****************************************************************************
-//
-
-INTN
-IsElfHeader (
-  UINT8  *FileBuffer
-)
-{
-  return (FileBuffer[EI_MAG0] == ELFMAG0 && 
-          FileBuffer[EI_MAG1] == ELFMAG1 &&
-          FileBuffer[EI_MAG2] == ELFMAG2 &&
-          FileBuffer[EI_MAG3] == ELFMAG3);
-}
-
-BOOLEAN
-ConvertElf (
-  UINT8  **FileBuffer,
-  UINT32 *FileLength
-  )
-{
-  ELF_FUNCTION_TABLE              ElfFunctions;
-  UINT8                           EiClass;
-
-  //
-  // Determine ELF type and set function table pointer correctly.
-  //
-  VerboseMsg ("Check Efl Image Header");
-  EiClass = (*FileBuffer)[EI_CLASS];
-  if (EiClass == ELFCLASS32) {
-    if (!InitializeElf32 (*FileBuffer, &ElfFunctions)) {
-      return FALSE;
-    }
-  } else if (EiClass == ELFCLASS64) {
-    if (!InitializeElf64 (*FileBuffer, &ElfFunctions)) {
-      return FALSE;
-    }
-  } else {
-    Error (NULL, 0, 3000, "Unsupported", "ELF EI_CLASS not supported.");
-    return FALSE;
-  }
-
-  //
-  // Compute sections new address.
-  //  
-  VerboseMsg ("Compute sections new address.");
-  ElfFunctions.ScanSections ();
-
-  //
-  // Write and relocate sections.
-  //
-  VerboseMsg ("Write and relocate sections.");
-  ElfFunctions.WriteSections (SECTION_TEXT);
-  ElfFunctions.WriteSections (SECTION_DATA);
-  ElfFunctions.WriteSections (SECTION_HII);
-
-  //
-  // Translate and write relocations.
-  //
-  VerboseMsg ("Translate and write relocations.");
-  ElfFunctions.WriteRelocations ();
-
-  //
-  // Write debug info.
-  //
-  VerboseMsg ("Write debug info.");
-  ElfFunctions.WriteDebug ();
-
-  //
-  // Make sure image size is correct before returning the new image.
-  //
-  VerboseMsg ("Set image size.");
-  ElfFunctions.SetImageSize ();
-
-  //
-  // Replace.
-  //
-  free (*FileBuffer);
-  *FileBuffer = mCoffFile;
-  *FileLength = mCoffOffset;
-
-  //
-  // Free resources used by ELF functions.
-  //
-  ElfFunctions.CleanUp ();
-  
-  return TRUE;
-}
+/** @file\r
+Elf convert solution\r
+\r
+Copyright (c) 2010 - 2018, Intel Corporation. All rights reserved.<BR>\r
+\r
+SPDX-License-Identifier: BSD-2-Clause-Patent\r
+\r
+**/\r
+\r
+#include "WinNtInclude.h"\r
+\r
+#ifndef __GNUC__\r
+#include <windows.h>\r
+#include <io.h>\r
+#endif\r
+#include <stdio.h>\r
+#include <stdlib.h>\r
+#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
+\r
+#include "EfiUtilityMsgs.h"\r
+\r
+#include "GenFw.h"\r
+#include "ElfConvert.h"\r
+#include "Elf32Convert.h"\r
+#include "Elf64Convert.h"\r
+\r
+//\r
+// Result Coff file in memory.\r
+//\r
+UINT8 *mCoffFile = NULL;\r
+\r
+//\r
+// COFF relocation data\r
+//\r
+EFI_IMAGE_BASE_RELOCATION *mCoffBaseRel;\r
+UINT16                    *mCoffEntryRel;\r
+\r
+//\r
+// Current offset in coff file.\r
+//\r
+UINT32 mCoffOffset;\r
+\r
+//\r
+// Offset in Coff file of headers and sections.\r
+//\r
+UINT32 mTableOffset;\r
+\r
+//\r
+//mFileBufferSize\r
+//\r
+UINT32 mFileBufferSize;\r
+\r
+//\r
+//*****************************************************************************\r
+// Common ELF Functions\r
+//*****************************************************************************\r
+//\r
+\r
+VOID\r
+CoffAddFixupEntry(\r
+  UINT16 Val\r
+  )\r
+{\r
+  *mCoffEntryRel = Val;\r
+  mCoffEntryRel++;\r
+  mCoffBaseRel->SizeOfBlock += 2;\r
+  mCoffOffset += 2;\r
+}\r
+\r
+VOID\r
+CoffAddFixup(\r
+  UINT32 Offset,\r
+  UINT8  Type\r
+  )\r
+{\r
+  if (mCoffBaseRel == NULL\r
+      || mCoffBaseRel->VirtualAddress != (Offset & ~0xfff)) {\r
+    if (mCoffBaseRel != NULL) {\r
+      //\r
+      // Add a null entry (is it required ?)\r
+      //\r
+      CoffAddFixupEntry (0);\r
+\r
+      //\r
+      // Pad for alignment.\r
+      //\r
+      if (mCoffOffset % 4 != 0)\r
+        CoffAddFixupEntry (0);\r
+    }\r
+\r
+    mCoffFile = realloc (\r
+      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
+      );\r
+\r
+    mCoffBaseRel = (EFI_IMAGE_BASE_RELOCATION*)(mCoffFile + mCoffOffset);\r
+    mCoffBaseRel->VirtualAddress = Offset & ~0xfff;\r
+    mCoffBaseRel->SizeOfBlock = sizeof(EFI_IMAGE_BASE_RELOCATION);\r
+\r
+    mCoffEntryRel = (UINT16 *)(mCoffBaseRel + 1);\r
+    mCoffOffset += sizeof(EFI_IMAGE_BASE_RELOCATION);\r
+  }\r
+\r
+  //\r
+  // Fill the entry.\r
+  //\r
+  CoffAddFixupEntry((UINT16) ((Type << 12) | (Offset & 0xfff)));\r
+}\r
+\r
+VOID\r
+CreateSectionHeader (\r
+  const CHAR8 *Name,\r
+  UINT32      Offset,\r
+  UINT32      Size,\r
+  UINT32      Flags\r
+  )\r
+{\r
+  EFI_IMAGE_SECTION_HEADER *Hdr;\r
+  Hdr = (EFI_IMAGE_SECTION_HEADER*)(mCoffFile + mTableOffset);\r
+\r
+  strcpy((char *)Hdr->Name, Name);\r
+  Hdr->Misc.VirtualSize = Size;\r
+  Hdr->VirtualAddress = Offset;\r
+  Hdr->SizeOfRawData = Size;\r
+  Hdr->PointerToRawData = Offset;\r
+  Hdr->PointerToRelocations = 0;\r
+  Hdr->PointerToLinenumbers = 0;\r
+  Hdr->NumberOfRelocations = 0;\r
+  Hdr->NumberOfLinenumbers = 0;\r
+  Hdr->Characteristics = Flags;\r
+\r
+  mTableOffset += sizeof (EFI_IMAGE_SECTION_HEADER);\r
+}\r
+\r
+//\r
+//*****************************************************************************\r
+// Functions called from GenFw main code.\r
+//*****************************************************************************\r
+//\r
+\r
+INTN\r
+IsElfHeader (\r
+  UINT8  *FileBuffer\r
+)\r
+{\r
+  return (FileBuffer[EI_MAG0] == ELFMAG0 &&\r
+          FileBuffer[EI_MAG1] == ELFMAG1 &&\r
+          FileBuffer[EI_MAG2] == ELFMAG2 &&\r
+          FileBuffer[EI_MAG3] == ELFMAG3);\r
+}\r
+\r
+BOOLEAN\r
+ConvertElf (\r
+  UINT8  **FileBuffer,\r
+  UINT32 *FileLength\r
+  )\r
+{\r
+  ELF_FUNCTION_TABLE              ElfFunctions;\r
+  UINT8                           EiClass;\r
+\r
+  mFileBufferSize = *FileLength;\r
+  //\r
+  // Determine ELF type and set function table pointer correctly.\r
+  //\r
+  VerboseMsg ("Check Elf Image Header");\r
+  EiClass = (*FileBuffer)[EI_CLASS];\r
+  if (EiClass == ELFCLASS32) {\r
+    if (!InitializeElf32 (*FileBuffer, &ElfFunctions)) {\r
+      return FALSE;\r
+    }\r
+  } else if (EiClass == ELFCLASS64) {\r
+    if (!InitializeElf64 (*FileBuffer, &ElfFunctions)) {\r
+      return FALSE;\r
+    }\r
+  } else {\r
+    Error (NULL, 0, 3000, "Unsupported", "ELF EI_CLASS not supported.");\r
+    return FALSE;\r
+  }\r
+\r
+  //\r
+  // Compute sections new address.\r
+  //\r
+  VerboseMsg ("Compute sections new address.");\r
+  ElfFunctions.ScanSections ();\r
+\r
+  //\r
+  // Write and relocate sections.\r
+  //\r
+  VerboseMsg ("Write and relocate sections.");\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
+  //\r
+  VerboseMsg ("Translate and write relocations.");\r
+  ElfFunctions.WriteRelocations ();\r
+\r
+  //\r
+  // Write debug info.\r
+  //\r
+  VerboseMsg ("Write debug info.");\r
+  ElfFunctions.WriteDebug ();\r
+\r
+  //\r
+  // For PRM Driver to Write export info.\r
+  //\r
+  if (mExportFlag) {\r
+    VerboseMsg ("Write export info.");\r
+    ElfFunctions.WriteExport ();\r
+  }\r
+\r
+  //\r
+  // Make sure image size is correct before returning the new image.\r
+  //\r
+  VerboseMsg ("Set image size.");\r
+  ElfFunctions.SetImageSize ();\r
+\r
+  //\r
+  // Replace.\r
+  //\r
+  free (*FileBuffer);\r
+  *FileBuffer = mCoffFile;\r
+  *FileLength = mCoffOffset;\r
+\r
+  //\r
+  // Free resources used by ELF functions.\r
+  //\r
+  ElfFunctions.CleanUp ();\r
+\r
+  return TRUE;\r
+}\r