1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074 |
- /*++
- Copyright (c) 2014 Minoca Corp. All Rights Reserved
- Module Name:
- elfc32.c
- Abstract:
- This module implements support for converting an ELF32 image to a PE
- image.
- Author:
- Evan Green 7-Mar-2014
- Environment:
- Build
- --*/
- //
- // ------------------------------------------------------------------- Includes
- //
- #include <assert.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <time.h>
- #include "uefifw.h"
- #include "elfconv.h"
- //
- // --------------------------------------------------------------------- Macros
- //
- //
- // This macro aligns a value up to the COFF alignment.
- //
- #define ELFCONV_COFF_ALIGN(_Value) \
- (((_Value) + (ELFCONV_COFF_ALIGNMENT - 1)) & ~(ELFCONV_COFF_ALIGNMENT - 1))
- //
- // This macro returns a pointer to the base of the section header array given
- // a pointer to the ELF header.
- //
- #define ELFCONV_SECTION_BASE(_ElfHeader) \
- ((VOID *)(_ElfHeader) + (_ElfHeader)->e_shoff)
- //
- // This macro returns a pointer to the requested ELF section given the ELF
- // file header.
- //
- #define ELFCONV_ELF_SECTION(_ElfHeader, _SectionIndex) \
- ((VOID *)ELFCONV_SECTION_BASE(_ElfHeader) + \
- ((_SectionIndex) * (_ElfHeader)->e_shentsize))
- //
- // This macro returns a pointer to the base of the program header array given
- // a pointer to the ELF header.
- //
- #define ELFCONV_PROGRAM_HEADER_BASE(_ElfHeader) \
- ((VOID *)(_ElfHeader) + (_ElfHeader)->e_phoff)
- //
- // This macro returns a pointer to the requested ELF program header given the
- // ELF file header.
- //
- #define ELFCONV_ELF_PROGRAM_HEADER(_ElfHeader, _ProgramHeaderIndex) \
- ((VOID *)ELFCONV_PROGRAM_HEADER_BASE(_ElfHeader) + \
- ((_ProgramHeaderIndex) * (_ElfHeader)->e_phentsize))
- //
- // ---------------------------------------------------------------- Definitions
- //
- //
- // Define the number of sections assumed in the PE image.
- //
- #define ELFCONV_PE_SECTION_COUNT 16
- //
- // Define the alignment used throughout the COFF file.
- //
- #define ELFCONV_COFF_ALIGNMENT 0x20
- //
- // ------------------------------------------------------ Data Type Definitions
- //
- typedef
- BOOLEAN
- (*PELFCONV_SECTION_FILTER_FUNCTION) (
- Elf32_Ehdr *ElfHeader,
- Elf32_Shdr *SectionHeader
- );
- /*++
- Routine Description:
- This routine filters ELF sections based on certain criteria.
- Arguments:
- ElfHeader - Supplies a pointer to the ELF file header.
- SectionHeader - Supplies a pointer to the section header in question.
- Return Value:
- TRUE if the section matches the filter.
- FALSE if the section does not match the filter.
- --*/
- //
- // ----------------------------------------------- Internal Function Prototypes
- //
- BOOLEAN
- ElfconvScanSections32 (
- PELFCONV_CONTEXT Context
- );
- BOOLEAN
- ElfconvWriteSections32 (
- PELFCONV_CONTEXT Context,
- ELFCONV_SECTION_FILTER FilterType
- );
- BOOLEAN
- ElfconvWriteRelocations32 (
- PELFCONV_CONTEXT Context
- );
- BOOLEAN
- ElfconvWriteDebug32 (
- PELFCONV_CONTEXT Context
- );
- VOID
- ElfconvSetImageSize32 (
- PELFCONV_CONTEXT Context
- );
- VOID
- ElfconvCleanUp32 (
- PELFCONV_CONTEXT Context
- );
- BOOLEAN
- ElfconvIsTextSection (
- Elf32_Ehdr *ElfHeader,
- Elf32_Shdr *SectionHeader
- );
- BOOLEAN
- ElfconvIsDataSection (
- Elf32_Ehdr *ElfHeader,
- Elf32_Shdr *SectionHeader
- );
- BOOLEAN
- ElfconvIsHiiRsrcSection (
- Elf32_Ehdr *ElfHeader,
- Elf32_Shdr *SectionHeader
- );
- BOOLEAN
- ElfconvIsDebugSection (
- Elf32_Ehdr *ElfHeader,
- Elf32_Shdr *SectionHeader
- );
- VOID
- ElfconvThumbMovtImmediatePatch (
- UINT16 *Instruction,
- UINT16 Address
- );
- BOOLEAN
- ElfconvConvertElfAddress (
- PELFCONV_CONTEXT Context,
- UINT32 *Address
- );
- //
- // -------------------------------------------------------------------- Globals
- //
- //
- // ------------------------------------------------------------------ Functions
- //
- BOOLEAN
- ElfconvInitializeElf32 (
- PELFCONV_CONTEXT Context,
- PELFCONV_FUNCTION_TABLE FunctionTable
- )
- /*++
- Routine Description:
- This routine attempts to bind an ELF conversion context to an ELF32 image.
- Arguments:
- Context - Supplies a pointer to the ELF context.
- FunctionTable - Supplies a pointer to the ELF conversion functions on
- success.
- Return Value:
- TRUE on success.
- FALSE if the image is not a valid ELF32 image.
- --*/
- {
- Elf32_Ehdr *ElfHeader;
- BOOLEAN Result;
- Result = FALSE;
- ElfHeader = Context->InputFile;
- if (Context->InputFileSize < sizeof(Elf32_Ehdr)) {
- if ((ElfHeader->e_ident[EI_CLASS] != ELFCLASS32) ||
- (ElfHeader->e_ident[EI_DATA] != ELFDATA2LSB) ||
- ((ElfHeader->e_type != ET_EXEC) && (ElfHeader->e_type != ET_DYN)) ||
- ((ElfHeader->e_machine != EM_386) &&
- (ElfHeader->e_machine != EM_ARM)) ||
- (ElfHeader->e_version != EV_CURRENT)) {
- fprintf(stderr, "ELF Image not valid.\n");
- goto InitializeElf32End;
- }
- }
- Context->CoffSectionsOffset = malloc(ElfHeader->e_shnum * sizeof(UINT32));
- if (Context->CoffSectionsOffset == NULL) {
- goto InitializeElf32End;
- }
- memset(Context->CoffSectionsOffset, 0, ElfHeader->e_shnum * sizeof(UINT32));
- FunctionTable->ScanSections = ElfconvScanSections32;
- FunctionTable->WriteSections = ElfconvWriteSections32;
- FunctionTable->WriteRelocations = ElfconvWriteRelocations32;
- FunctionTable->WriteDebug = ElfconvWriteDebug32;
- FunctionTable->SetImageSize = ElfconvSetImageSize32;
- FunctionTable->CleanUp = ElfconvCleanUp32;
- Result = TRUE;
- InitializeElf32End:
- return Result;
- }
- //
- // --------------------------------------------------------- Internal Functions
- //
- BOOLEAN
- ElfconvScanSections32 (
- PELFCONV_CONTEXT Context
- )
- /*++
- Routine Description:
- This routine scans the ELF sections and sets up the PE image.
- Arguments:
- Context - Supplies a pointer to the ELF context.
- Return Value:
- TRUE on success.
- FALSE on failure.
- --*/
- {
- UINT32 CoffEntry;
- EFI_IMAGE_DOS_HEADER *DosHeader;
- Elf32_Ehdr *ElfHeader;
- Elf32_Shdr *ElfSection;
- UINT32 Flags;
- BOOLEAN FoundText;
- EFI_IMAGE_OPTIONAL_HEADER_UNION *NtHeader;
- UINTN SectionIndex;
- CHAR8 *SectionName;
- Elf32_Shdr *StringSection;
- CoffEntry = 0;
- Context->CoffOffset = 0;
- Context->TextOffset = 0;
- Context->CoffOffset = sizeof(EFI_IMAGE_DOS_HEADER) + 0x40;
- Context->NtHeaderOffset = Context->CoffOffset;
- FoundText = FALSE;
- ElfHeader = Context->InputFile;
- assert((ElfHeader->e_machine == EM_386) ||
- (ElfHeader->e_machine == EM_ARM));
- Context->CoffOffset += sizeof(EFI_IMAGE_NT_HEADERS32);
- Context->TableOffset = Context->CoffOffset;
- Context->CoffOffset += ELFCONV_PE_SECTION_COUNT *
- sizeof(EFI_IMAGE_SECTION_HEADER);
- //
- // Find and wrangle any text sections.
- //
- Context->CoffOffset = ELFCONV_COFF_ALIGN(Context->CoffOffset);
- for (SectionIndex = 0;
- SectionIndex < ElfHeader->e_shnum;
- SectionIndex += 1) {
- ElfSection = ELFCONV_ELF_SECTION(ElfHeader, SectionIndex);
- if (ElfconvIsTextSection(ElfHeader, ElfSection) != FALSE) {
- if ((Context->Flags & ELFCONV_OPTION_VERBOSE) != 0) {
- StringSection = ELFCONV_ELF_SECTION(ElfHeader,
- ElfHeader->e_shstrndx);
- SectionName = (CHAR8 *)ElfHeader + StringSection->sh_offset +
- ElfSection->sh_name;
- printf("Found text section %s: Offset 0x%x, size 0x%x.\n",
- SectionName,
- ElfSection->sh_offset,
- ElfSection->sh_size);
- }
- //
- // If the alignment field is valid, align the COFF offset up.
- //
- if ((ElfSection->sh_addralign != 0) &&
- (ElfSection->sh_addralign != 1)) {
- if ((ElfSection->sh_addr &
- (ElfSection->sh_addralign - 1)) == 0) {
- Context->CoffOffset = ALIGN_VALUE(Context->CoffOffset,
- ElfSection->sh_addralign);
- } else if ((ElfSection->sh_addr % ElfSection->sh_addralign) !=
- (Context->CoffOffset % ElfSection->sh_addralign)) {
- fprintf(stderr, "Error: Unsupported section alignment.\n");
- return FALSE;
- }
- }
- //
- // Relocate the entry point.
- //
- if ((ElfHeader->e_entry >= ElfSection->sh_addr) &&
- (ElfHeader->e_entry <
- ElfSection->sh_addr + ElfSection->sh_size)) {
- CoffEntry = Context->CoffOffset +
- ElfHeader->e_entry - ElfSection->sh_addr;
- }
- if (FoundText == FALSE) {
- Context->TextOffset = Context->CoffOffset;
- FoundText = TRUE;
- }
- Context->CoffSectionsOffset[SectionIndex] = Context->CoffOffset;
- Context->CoffOffset += ElfSection->sh_size;
- }
- }
- if (FoundText == FALSE) {
- fprintf(stderr, "Error: Failed to find a text section.\n");
- return FALSE;
- }
- if (ElfHeader->e_machine != EM_ARM) {
- Context->CoffOffset = ELFCONV_COFF_ALIGN(Context->CoffOffset);
- }
- //
- // Find and wrangle data sections.
- //
- Context->DataOffset = Context->CoffOffset;
- for (SectionIndex = 0;
- SectionIndex < ElfHeader->e_shnum;
- SectionIndex += 1) {
- ElfSection = ELFCONV_ELF_SECTION(ElfHeader, SectionIndex);
- if (ElfconvIsDataSection(ElfHeader, ElfSection) != FALSE) {
- if ((Context->Flags & ELFCONV_OPTION_VERBOSE) != 0) {
- StringSection = ELFCONV_ELF_SECTION(ElfHeader,
- ElfHeader->e_shstrndx);
- SectionName = (CHAR8 *)ElfHeader + StringSection->sh_offset +
- ElfSection->sh_name;
- printf("Found data section %s: Offset 0x%x, size 0x%x.\n",
- SectionName,
- ElfSection->sh_offset,
- ElfSection->sh_size);
- }
- //
- // If the alignment field is valid, align the COFF offset up.
- //
- if ((ElfSection->sh_addralign != 0) &&
- (ElfSection->sh_addralign != 1)) {
- if ((ElfSection->sh_addr &
- (ElfSection->sh_addralign - 1)) == 0) {
- Context->CoffOffset = ALIGN_VALUE(Context->CoffOffset,
- ElfSection->sh_addralign);
- } else if ((ElfSection->sh_addr % ElfSection->sh_addralign) !=
- (Context->CoffOffset % ElfSection->sh_addralign)) {
- fprintf(stderr, "Error: Unsupported section alignment.\n");
- return FALSE;
- }
- }
- Context->CoffSectionsOffset[SectionIndex] = Context->CoffOffset;
- Context->CoffOffset += ElfSection->sh_size;
- }
- }
- Context->CoffOffset = ELFCONV_COFF_ALIGN(Context->CoffOffset);
- //
- // Find and wrangle HII .rsrc section.
- //
- Context->HiiRsrcOffset = Context->CoffOffset;
- for (SectionIndex = 0;
- SectionIndex < ElfHeader->e_shnum;
- SectionIndex += 1) {
- ElfSection = ELFCONV_ELF_SECTION(ElfHeader, SectionIndex);
- if (ElfconvIsHiiRsrcSection(ElfHeader, ElfSection) != FALSE) {
- if ((Context->Flags & ELFCONV_OPTION_VERBOSE) != 0) {
- StringSection = ELFCONV_ELF_SECTION(ElfHeader,
- ElfHeader->e_shstrndx);
- SectionName = (CHAR8 *)ElfHeader + StringSection->sh_offset +
- ElfSection->sh_name;
- printf("Found rsrc section %s: Offset 0x%x, size 0x%x.\n",
- SectionName,
- ElfSection->sh_offset,
- ElfSection->sh_size);
- }
- //
- // If the alignment field is valid, align the COFF offset up.
- //
- if ((ElfSection->sh_addralign != 0) &&
- (ElfSection->sh_addralign != 1)) {
- if ((ElfSection->sh_addr &
- (ElfSection->sh_addralign - 1)) == 0) {
- Context->CoffOffset = ALIGN_VALUE(Context->CoffOffset,
- ElfSection->sh_addralign);
- } else if ((ElfSection->sh_addr % ElfSection->sh_addralign) !=
- (Context->CoffOffset % ElfSection->sh_addralign)) {
- fprintf(stderr, "Error: Unsupported section alignment.\n");
- return FALSE;
- }
- }
- if (ElfSection->sh_size != 0) {
- Context->CoffSectionsOffset[SectionIndex] = Context->CoffOffset;
- Context->CoffOffset += ElfSection->sh_size;
- Context->CoffOffset = ELFCONV_COFF_ALIGN(Context->CoffOffset);
- ElfconvSetHiiResourceHeader(
- (UINT8 *)ElfHeader + ElfSection->sh_offset,
- Context->HiiRsrcOffset);
- break;
- }
- }
- }
- Context->RelocationOffset = Context->CoffOffset;
- //
- // Allocate the base COFF file. This will be expanded later for relocations.
- //
- Context->CoffFile = malloc(Context->CoffOffset);
- if (Context->CoffFile == NULL) {
- return FALSE;
- }
- memset(Context->CoffFile, 0, Context->CoffOffset);
- //
- // Fill in the headers.
- //
- DosHeader = (EFI_IMAGE_DOS_HEADER *)(Context->CoffFile);
- DosHeader->e_magic = EFI_IMAGE_DOS_SIGNATURE;
- DosHeader->e_lfanew = Context->NtHeaderOffset;
- NtHeader = (EFI_IMAGE_OPTIONAL_HEADER_UNION *)(Context->CoffFile +
- Context->NtHeaderOffset);
- NtHeader->Pe32.Signature = EFI_IMAGE_NT_SIGNATURE;
- switch (ElfHeader->e_machine) {
- case EM_386:
- NtHeader->Pe32.FileHeader.Machine = EFI_IMAGE_MACHINE_IA32;
- break;
- case EM_ARM:
- NtHeader->Pe32.FileHeader.Machine = EFI_IMAGE_MACHINE_ARMTHUMB_MIXED;
- break;
- default:
- assert(FALSE);
- return FALSE;
- }
- NtHeader->Pe32.OptionalHeader.Magic = EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC;
- NtHeader->Pe32.FileHeader.NumberOfSections = ELFCONV_PE_SECTION_COUNT;
- NtHeader->Pe32.FileHeader.TimeDateStamp = (UINT32)time(NULL);
- Context->ImageTimestamp = NtHeader->Pe32.FileHeader.TimeDateStamp;
- NtHeader->Pe32.FileHeader.PointerToSymbolTable = 0;
- NtHeader->Pe32.FileHeader.NumberOfSymbols = 0;
- NtHeader->Pe32.FileHeader.SizeOfOptionalHeader =
- sizeof(NtHeader->Pe32.OptionalHeader);
- NtHeader->Pe32.FileHeader.Characteristics =
- EFI_IMAGE_FILE_EXECUTABLE_IMAGE |
- EFI_IMAGE_FILE_LINE_NUMS_STRIPPED |
- EFI_IMAGE_FILE_LOCAL_SYMS_STRIPPED |
- EFI_IMAGE_FILE_32BIT_MACHINE;
- NtHeader->Pe32.OptionalHeader.SizeOfCode = Context->DataOffset -
- Context->TextOffset;
- NtHeader->Pe32.OptionalHeader.SizeOfInitializedData =
- Context->RelocationOffset - Context->DataOffset;
- NtHeader->Pe32.OptionalHeader.SizeOfUninitializedData = 0;
- NtHeader->Pe32.OptionalHeader.AddressOfEntryPoint = CoffEntry;
- NtHeader->Pe32.OptionalHeader.BaseOfCode = Context->TextOffset;
- NtHeader->Pe32.OptionalHeader.BaseOfData = Context->DataOffset;
- NtHeader->Pe32.OptionalHeader.ImageBase = 0;
- NtHeader->Pe32.OptionalHeader.SectionAlignment = ELFCONV_COFF_ALIGNMENT;
- NtHeader->Pe32.OptionalHeader.FileAlignment = ELFCONV_COFF_ALIGNMENT;
- NtHeader->Pe32.OptionalHeader.SizeOfImage = 0;
- NtHeader->Pe32.OptionalHeader.SizeOfHeaders = Context->TextOffset;
- NtHeader->Pe32.OptionalHeader.Subsystem = Context->SubsystemType;
- NtHeader->Pe32.OptionalHeader.NumberOfRvaAndSizes =
- EFI_IMAGE_NUMBER_OF_DIRECTORY_ENTRIES;
- //
- // Create the section headers.
- //
- if (Context->DataOffset - Context->TextOffset > 0) {
- Flags = EFI_IMAGE_SCN_CNT_CODE |
- EFI_IMAGE_SCN_MEM_EXECUTE |
- EFI_IMAGE_SCN_MEM_READ;
- ElfconvCreateSectionHeader(Context,
- ".text",
- Context->TextOffset,
- Context->DataOffset - Context->TextOffset,
- Flags);
- } else {
- NtHeader->Pe32.FileHeader.NumberOfSections -= 1;
- }
- if (Context->HiiRsrcOffset - Context->DataOffset > 0) {
- Flags = EFI_IMAGE_SCN_CNT_INITIALIZED_DATA |
- EFI_IMAGE_SCN_MEM_WRITE |
- EFI_IMAGE_SCN_MEM_READ;
- ElfconvCreateSectionHeader(Context,
- ".data",
- Context->DataOffset,
- Context->HiiRsrcOffset - Context->DataOffset,
- Flags);
- } else {
- NtHeader->Pe32.FileHeader.NumberOfSections -= 1;
- }
- if (Context->RelocationOffset - Context->HiiRsrcOffset > 0) {
- Flags = EFI_IMAGE_SCN_CNT_INITIALIZED_DATA |
- EFI_IMAGE_SCN_MEM_READ;
- ElfconvCreateSectionHeader(
- Context,
- ".rsrc",
- Context->HiiRsrcOffset,
- Context->RelocationOffset - Context->HiiRsrcOffset,
- Flags);
- } else {
- NtHeader->Pe32.FileHeader.NumberOfSections -= 1;
- }
- return TRUE;
- }
- BOOLEAN
- ElfconvWriteSections32 (
- PELFCONV_CONTEXT Context,
- ELFCONV_SECTION_FILTER FilterType
- )
- /*++
- Routine Description:
- This routine writes certain sections to the output image.
- Arguments:
- Context - Supplies a pointer to the ELF context.
- FilterType - Supplies the type of sections to write.
- Return Value:
- TRUE on success.
- FALSE on failure.
- --*/
- {
- UINT16 Address;
- UINT32 AddressValue;
- VOID *Destination;
- Elf32_Ehdr *ElfHeader;
- Elf32_Shdr *ElfSection;
- PELFCONV_SECTION_FILTER_FUNCTION FilterFunction;
- Elf32_Rel *Relocation;
- UINT32 RelocationOffset;
- Elf32_Shdr *RelocationSection;
- UINT32 SectionIndex;
- UINT32 SectionOffset;
- Elf32_Sym *Symbol;
- Elf32_Shdr *SymbolSectionHeader;
- UINT8 *SymbolTable;
- Elf32_Shdr *SymbolTableSection;
- UINT8 *Target;
- ElfHeader = Context->InputFile;
- assert((ElfHeader->e_machine == EM_386) ||
- (ElfHeader->e_machine == EM_ARM));
- switch (FilterType) {
- case ElfconvSectionText:
- FilterFunction = ElfconvIsTextSection;
- break;
- case ElfconvSectionData:
- FilterFunction = ElfconvIsDataSection;
- break;
- case ElfconvSectionHii:
- FilterFunction = ElfconvIsHiiRsrcSection;
- break;
- default:
- assert(FALSE);
- return FALSE;
- }
- //
- // Copy the contents of the eligible sections.
- //
- for (SectionIndex = 0;
- SectionIndex < ElfHeader->e_shnum;
- SectionIndex += 1) {
- ElfSection = ELFCONV_ELF_SECTION(ElfHeader, SectionIndex);
- if (FilterFunction(ElfHeader, ElfSection) != FALSE) {
- Destination = Context->CoffFile +
- Context->CoffSectionsOffset[SectionIndex];
- switch (ElfSection->sh_type) {
- case SHT_PROGBITS:
- case SHT_DYNAMIC:
- case SHT_DYNSYM:
- case SHT_REL:
- case SHT_RELA:
- if ((Context->Flags & ELFCONV_OPTION_VERBOSE) != 0) {
- printf("Copying section from ELF offset %x, size %x to "
- "COFF offset %x.\n",
- ElfSection->sh_offset,
- ElfSection->sh_size,
- (UINTN)Destination - (UINTN)Context->CoffFile);
- }
- memcpy(Destination,
- (UINT8 *)ElfHeader + ElfSection->sh_offset,
- ElfSection->sh_size);
- break;
- case SHT_NOBITS:
- if ((Context->Flags & ELFCONV_OPTION_VERBOSE) != 0) {
- printf("Zeroing COFF offset %x, size %x",
- (UINTN)Destination - (UINTN)Context->CoffFile,
- ElfSection->sh_size);
- }
- memset(Destination, 0, ElfSection->sh_size);
- break;
- case SHT_SYMTAB:
- case SHT_STRTAB:
- case SHT_HASH:
- break;
- default:
- fprintf(stderr,
- "Warning: Unknown section type %x.\n",
- ElfSection->sh_type);
- break;
- }
- }
- }
- //
- // Now apply relocations.
- //
- for (SectionIndex = 0;
- SectionIndex < ElfHeader->e_shnum;
- SectionIndex += 1) {
- RelocationSection = ELFCONV_ELF_SECTION(ElfHeader, SectionIndex);
- //
- // Skip everything except relocation sections.
- //
- if ((RelocationSection->sh_type != SHT_REL) &&
- (RelocationSection->sh_type != SHT_RELA)) {
- continue;
- }
- //
- // This is a relocation section. Extract section information that the
- // relocations apply to.
- //
- ElfSection = ELFCONV_ELF_SECTION(ElfHeader, RelocationSection->sh_info);
- SectionOffset = Context->CoffSectionsOffset[SectionIndex];
- //
- // Skip the section if the filter function doesn't match or they're
- // addend relocations.
- //
- if ((RelocationSection->sh_type != SHT_REL) ||
- (FilterFunction(ElfHeader, ElfSection) == FALSE)) {
- continue;
- }
- //
- // Get the symbol table.
- //
- SymbolTableSection = ELFCONV_ELF_SECTION(ElfHeader,
- RelocationSection->sh_link);
- SymbolTable = (UINT8 *)ElfHeader + SymbolTableSection->sh_offset;
- //
- // Process all relocation entries for this section.
- //
- for (RelocationOffset = 0;
- RelocationOffset < RelocationSection->sh_size;
- RelocationOffset += RelocationSection->sh_entsize) {
- Relocation = (Elf32_Rel *)((UINT8 *)ElfHeader +
- RelocationSection->sh_offset +
- RelocationOffset);
- Symbol = (Elf32_Sym *)(SymbolTable +
- (ELF32_R_SYM(Relocation->r_info) *
- SymbolTableSection->sh_entsize));
- //
- // Skip absolute symbols.
- //
- if (Symbol->st_shndx == SHN_ABS) {
- continue;
- }
- if ((Symbol->st_shndx == SHN_UNDEF) ||
- (Symbol->st_shndx > ElfHeader->e_shnum)) {
- fprintf(stderr,
- "Error: Invalid symbol definition %x, "
- "relocation section %d, offset %x.\n",
- Symbol->st_shndx,
- SectionIndex,
- (UINTN)Relocation - (UINTN)ElfHeader);
- return FALSE;
- }
- SymbolSectionHeader = ELFCONV_ELF_SECTION(ElfHeader,
- Symbol->st_shndx);
- //
- // Convert the relocation data to a pointer into the COFF file.
- // r_offset is the virtual address of the storage unit to be
- // relocated. sh_addr is the virtual address for the base of the
- // section.
- //
- Target = Context->CoffFile + SectionOffset +
- (Relocation->r_offset - ElfSection->sh_addr);
- //
- // The relocation types are machine dependent.
- //
- if (ElfHeader->e_machine == EM_386) {
- switch (ELF32_R_TYPE(Relocation->r_info)) {
- case R_386_NONE:
- break;
- //
- // This is an absolute relocation. Convert the target from an
- // absolute virtuall address to an absolute COFF address.
- //
- case R_386_32:
- *(UINT32 *)Target =
- *(UINT32 *)Target -
- SymbolSectionHeader->sh_addr +
- Context->CoffSectionsOffset[Symbol->st_shndx];
- break;
- //
- // This is a relative relocation: Symbol - PC + Addend.
- //
- case R_386_PC32:
- *(UINT32 *)Target =
- *(UINT32 *)Target +
- (Context->CoffSectionsOffset[Symbol->st_shndx] -
- SymbolSectionHeader->sh_addr) -
- (SectionOffset - ElfSection->sh_addr);
- break;
- default:
- fprintf(stderr,
- "Error: Unsupported relocation type %d.\n",
- ELF32_R_TYPE(Relocation->r_info));
- return FALSE;
- }
- } else if (ElfHeader->e_machine == EM_ARM) {
- switch (ELF32_R_TYPE(Relocation->r_info)) {
- //
- // PC-relative relocations don't need modification.
- //
- case R_ARM_RBASE:
- case R_ARM_PC24:
- case R_ARM_REL32:
- case R_ARM_XPC25:
- case R_ARM_THM_PC22:
- case R_ARM_THM_JUMP19:
- case R_ARM_CALL:
- case R_ARM_JMP24:
- case R_ARM_THM_JUMP24:
- case R_ARM_PREL31:
- case R_ARM_MOVW_PREL_NC:
- case R_ARM_MOVT_PREL:
- case R_ARM_THM_MOVW_PREL_NC:
- case R_ARM_THM_MOVT_PREL:
- case R_ARM_THM_JMP6:
- case R_ARM_THM_ALU_PREL_11_0:
- case R_ARM_THM_PC12:
- case R_ARM_REL32_NOI:
- case R_ARM_ALU_PC_G0_NC:
- case R_ARM_ALU_PC_G0:
- case R_ARM_ALU_PC_G1_NC:
- case R_ARM_ALU_PC_G1:
- case R_ARM_ALU_PC_G2:
- case R_ARM_LDR_PC_G1:
- case R_ARM_LDR_PC_G2:
- case R_ARM_LDRS_PC_G0:
- case R_ARM_LDRS_PC_G1:
- case R_ARM_LDRS_PC_G2:
- case R_ARM_LDC_PC_G0:
- case R_ARM_LDC_PC_G1:
- case R_ARM_LDC_PC_G2:
- case R_ARM_GOT_PREL:
- case R_ARM_THM_JUMP11:
- case R_ARM_THM_JUMP8:
- case R_ARM_TLS_GD32:
- case R_ARM_TLS_LDM32:
- case R_ARM_TLS_IE32:
- case R_ARM_GOT_BREL:
- case R_ARM_BASE_PREL:
- break;
- //
- // MOVW relocates the lower 16 bits of the address.
- //
- case R_ARM_THM_MOVW_ABS_NC:
- Address =
- (UINT16)(Symbol->st_value -
- SymbolSectionHeader->sh_addr +
- Context->CoffSectionsOffset[Symbol->st_shndx]);
- ElfconvThumbMovtImmediatePatch((UINT16 *)Target, Address);
- break;
- //
- // MOVT relocates the upper 16 bits of the address.
- //
- case R_ARM_THM_MOVT_ABS:
- AddressValue =
- (Symbol->st_value -
- SymbolSectionHeader->sh_addr +
- Context->CoffSectionsOffset[Symbol->st_shndx]) >> 16;
- Address = (UINT16)AddressValue;
- ElfconvThumbMovtImmediatePatch((UINT16 *)Target, Address);
- break;
- //
- // This is a 32 bit absolute relocation.
- //
- case R_ARM_ABS32:
- case R_ARM_RABS32:
- *(UINT32 *)Target =
- *(UINT32 *)Target -
- SymbolSectionHeader->sh_addr +
- Context->CoffSectionsOffset[Symbol->st_shndx];
- break;
- default:
- fprintf(stderr,
- "Error: Unsupported relocation type %d.\n",
- ELF32_R_TYPE(Relocation->r_info));
- return FALSE;
- }
- //
- // The machine type is unknown. Execution should have failed before
- // this point.
- //
- } else {
- assert(FALSE);
- return FALSE;
- }
- }
- }
- return TRUE;
- }
- BOOLEAN
- ElfconvWriteRelocations32 (
- PELFCONV_CONTEXT Context
- )
- /*++
- Routine Description:
- This routine converts the ELF relocations into PE relocations and writes
- them into the output PE file buffer.
- Arguments:
- Context - Supplies a pointer to the ELF context.
- Return Value:
- TRUE on success.
- FALSE on failure.
- --*/
- {
- UINT32 DestinationSectionIndex;
- EFI_IMAGE_DATA_DIRECTORY *Directories;
- EFI_IMAGE_DATA_DIRECTORY *Directory;
- Elf32_Dyn *DynamicSection;
- Elf32_Phdr *DynamicSegment;
- Elf32_Ehdr *ElfHeader;
- UINT32 FixupOffset;
- UINT32 Flags;
- BOOLEAN FoundRelocations;
- UINTN MovwOffset;
- EFI_IMAGE_OPTIONAL_HEADER_UNION *NtHeader;
- Elf32_Rel *Relocation;
- UINTN RelocationElementSize;
- UINT32 RelocationIndex;
- UINTN RelocationOffset;
- Elf32_Shdr *RelocationSectionHeader;
- UINTN RelocationSize;
- BOOLEAN Result;
- Elf32_Shdr *SectionHeader;
- UINT32 SectionIndex;
- UINT32 TargetAddress;
- UINT8 *TargetPointer;
- UINT32 TargetValue;
- ElfHeader = Context->InputFile;
- assert((ElfHeader->e_machine == EM_386) ||
- (ElfHeader->e_machine == EM_ARM));
- MovwOffset = 0;
- //
- // Loop across all sections looking for relocation sections.
- //
- FoundRelocations = FALSE;
- for (SectionIndex = 0;
- SectionIndex < ElfHeader->e_shnum;
- SectionIndex += 1) {
- RelocationSectionHeader = ELFCONV_ELF_SECTION(ElfHeader, SectionIndex);
- //
- // Skip things that aren't relocation sections.
- //
- if ((RelocationSectionHeader->sh_type != SHT_REL) &&
- (RelocationSectionHeader->sh_type != SHT_RELA)) {
- continue;
- }
- SectionHeader = ELFCONV_ELF_SECTION(ElfHeader,
- RelocationSectionHeader->sh_info);
- if ((Context->Flags & ELFCONV_OPTION_VERBOSE) != 0) {
- printf("Found relocation section (index %d).\n", SectionIndex);
- }
- //
- // Skip sections that are neither text nor data.
- //
- if ((ElfconvIsTextSection(ElfHeader, SectionHeader) == FALSE) &&
- (ElfconvIsDataSection(ElfHeader, SectionHeader) == FALSE)) {
- continue;
- }
- FoundRelocations = TRUE;
- for (RelocationIndex = 0;
- RelocationIndex < RelocationSectionHeader->sh_size;
- RelocationIndex += RelocationSectionHeader->sh_entsize) {
- Relocation = (Elf32_Rel *)((UINT8 *)ElfHeader +
- RelocationSectionHeader->sh_offset +
- RelocationIndex);
- if (ElfHeader->e_machine == EM_386) {
- switch (ELF32_R_TYPE(Relocation->r_info)) {
- case R_386_NONE:
- case R_386_PC32:
- break;
- //
- // Create a relative relocation entry from the absolute entry.
- //
- case R_386_32:
- DestinationSectionIndex = RelocationSectionHeader->sh_info;
- FixupOffset =
- Context->CoffSectionsOffset[DestinationSectionIndex] +
- (Relocation->r_offset - SectionHeader->sh_addr);
- ElfconvCoffAddFixup(Context,
- FixupOffset,
- EFI_IMAGE_REL_BASED_HIGHLOW);
- break;
- default:
- fprintf(stderr,
- "Error: Unsupported relocation type %d.\n",
- ELF32_R_TYPE(Relocation->r_info));
- return FALSE;
- }
- } else if (ElfHeader->e_machine == EM_ARM) {
- switch (ELF32_R_TYPE(Relocation->r_info)) {
- //
- // PC-relative relocations don't need modification.
- //
- case R_ARM_RBASE:
- case R_ARM_PC24:
- case R_ARM_REL32:
- case R_ARM_XPC25:
- case R_ARM_THM_PC22:
- case R_ARM_THM_JUMP19:
- case R_ARM_CALL:
- case R_ARM_JMP24:
- case R_ARM_THM_JUMP24:
- case R_ARM_PREL31:
- case R_ARM_MOVW_PREL_NC:
- case R_ARM_MOVT_PREL:
- case R_ARM_THM_MOVW_PREL_NC:
- case R_ARM_THM_MOVT_PREL:
- case R_ARM_THM_JMP6:
- case R_ARM_THM_ALU_PREL_11_0:
- case R_ARM_THM_PC12:
- case R_ARM_REL32_NOI:
- case R_ARM_ALU_PC_G0_NC:
- case R_ARM_ALU_PC_G0:
- case R_ARM_ALU_PC_G1_NC:
- case R_ARM_ALU_PC_G1:
- case R_ARM_ALU_PC_G2:
- case R_ARM_LDR_PC_G1:
- case R_ARM_LDR_PC_G2:
- case R_ARM_LDRS_PC_G0:
- case R_ARM_LDRS_PC_G1:
- case R_ARM_LDRS_PC_G2:
- case R_ARM_LDC_PC_G0:
- case R_ARM_LDC_PC_G1:
- case R_ARM_LDC_PC_G2:
- case R_ARM_GOT_PREL:
- case R_ARM_THM_JUMP11:
- case R_ARM_THM_JUMP8:
- case R_ARM_TLS_GD32:
- case R_ARM_TLS_LDM32:
- case R_ARM_TLS_IE32:
- case R_ARM_GOT_BREL:
- case R_ARM_BASE_PREL:
- break;
- //
- // MOVW relocates the lower 16 bits of the address.
- //
- case R_ARM_THM_MOVW_ABS_NC:
- DestinationSectionIndex = RelocationSectionHeader->sh_info;
- FixupOffset =
- Context->CoffSectionsOffset[DestinationSectionIndex] +
- (Relocation->r_offset - SectionHeader->sh_addr);
- Result = ElfconvCoffAddFixup(
- Context,
- FixupOffset,
- EFI_IMAGE_REL_BASED_ARM_MOV32T);
- if (Result == FALSE) {
- return FALSE;
- }
- //
- // PE/COFF tracks MOVW/MOVT relocations as a single
- // 64-bit instruction. Track this address so that an error
- // can be logged if the next relocation is not a MOVT.
- //
- MovwOffset = FixupOffset;
- break;
- //
- // MOVT relocates the upper 16 bits of the address. This is
- // already handled by the MOVW fixup.
- //
- case R_ARM_THM_MOVT_ABS:
- DestinationSectionIndex = RelocationSectionHeader->sh_info;
- FixupOffset =
- Context->CoffSectionsOffset[DestinationSectionIndex] +
- (Relocation->r_offset - SectionHeader->sh_addr);
- if (FixupOffset != MovwOffset + 4) {
- fprintf(stderr,
- "Error: PE requires MOVW+MOVT instruction "
- "sequences together.\n");
- return FALSE;
- }
- break;
- //
- // This is a 32 bit absolute relocation.
- //
- case R_ARM_ABS32:
- case R_ARM_RABS32:
- DestinationSectionIndex = RelocationSectionHeader->sh_info;
- FixupOffset =
- Context->CoffSectionsOffset[DestinationSectionIndex] +
- (Relocation->r_offset - SectionHeader->sh_addr);
- Result = ElfconvCoffAddFixup(Context,
- FixupOffset,
- EFI_IMAGE_REL_BASED_HIGHLOW);
- if (Result == FALSE) {
- return FALSE;
- }
- break;
- default:
- fprintf(stderr,
- "Error: Unsupported ARM relocation type %d.\n",
- ELF32_R_TYPE(Relocation->r_info));
- return FALSE;
- }
- } else {
- assert(FALSE);
- return FALSE;
- }
- }
- }
- //
- // If relocations were not found, try again looking for PT_DYNAMIC instead
- // of SHT_REL.
- //
- if (FoundRelocations == FALSE) {
- for (SectionIndex = 0;
- SectionIndex < ElfHeader->e_phnum;
- SectionIndex += 1) {
- RelocationElementSize = 0;
- RelocationSize = 0;
- RelocationOffset = 0;
- DynamicSegment =
- ELFCONV_ELF_PROGRAM_HEADER(ElfHeader, SectionIndex);
- if (DynamicSegment->p_type != PT_DYNAMIC) {
- continue;
- }
- if ((Context->Flags & ELFCONV_OPTION_VERBOSE) != 0) {
- printf("Found dynamic section (index %d)\n", SectionIndex);
- }
- DynamicSection =
- (Elf32_Dyn *)((UINT8 *)ElfHeader + DynamicSegment->p_offset);
- while (DynamicSection->d_tag != DT_NULL) {
- switch (DynamicSection->d_tag) {
- case DT_REL:
- RelocationOffset = DynamicSection->d_un.d_val;
- if ((Context->Flags & ELFCONV_OPTION_VERBOSE) != 0) {
- printf("Relocation offset %x.\n", RelocationOffset);
- }
- Result = ElfconvConvertElfAddress(Context,
- &RelocationOffset);
- if (Result == FALSE) {
- fprintf(stderr,
- "Error: Failed to convert dynamic relocation "
- "address %x to destination image offset.\n",
- RelocationOffset);
- return FALSE;
- }
- if ((Context->Flags & ELFCONV_OPTION_VERBOSE) != 0) {
- printf("Adjusted relocation offset %x.\n",
- RelocationOffset);
- }
- break;
- case DT_RELSZ:
- RelocationSize = DynamicSection->d_un.d_val;
- if ((Context->Flags & ELFCONV_OPTION_VERBOSE) != 0) {
- printf("Relocation size %x.\n", RelocationSize);
- }
- break;
- case DT_RELENT:
- RelocationElementSize = DynamicSection->d_un.d_val;
- if ((Context->Flags & ELFCONV_OPTION_VERBOSE) != 0) {
- printf("Relocation element size %x.\n",
- RelocationElementSize);
- }
- break;
- default:
- break;
- }
- DynamicSection += 1;
- }
- if ((Context->Flags & ELFCONV_OPTION_VERBOSE) != 0) {
- printf("Relocations: PHDR %d (p_offset %x) Offset %x Size %x "
- "ElemSize %x\n",
- SectionIndex,
- DynamicSegment->p_offset,
- RelocationOffset,
- RelocationSize,
- RelocationElementSize);
- }
- if ((RelocationOffset == 0) || (RelocationSize == 0) ||
- (RelocationElementSize == 0)) {
- fprintf(stderr, "Error: Bad ARM relocations.\n");
- return FALSE;
- }
- for (RelocationIndex = 0;
- RelocationIndex < RelocationSize;
- RelocationIndex += RelocationElementSize) {
- Relocation = (Elf32_Rel *)(Context->CoffFile +
- RelocationOffset +
- RelocationIndex);
- if (ElfHeader->e_machine == EM_386) {
- switch (ELF32_R_TYPE(Relocation->r_info)) {
- //
- // Relative relocations contain a default VA in them. First
- // convert that VA into a COFF offset, then create a
- // relocation for it. If the value conversion failed, it's
- // probably a relocation off in a stabs section, so ignore
- // it.
- //
- case R_386_RELATIVE:
- TargetAddress = Relocation->r_offset;
- Result = ElfconvConvertElfAddress(Context,
- &TargetAddress);
- if (Result == FALSE) {
- fprintf(stderr,
- "R_386_RELATIVE target convert failed.\n");
- return FALSE;
- }
- TargetPointer = Context->CoffFile + TargetAddress;
- TargetValue = *(UINT32 *)TargetPointer;
- Result = ElfconvConvertElfAddress(Context,
- &TargetValue);
- if (Result == FALSE) {
- if ((Context->Flags &
- ELFCONV_OPTION_VERBOSE) != 0) {
- printf("Skipping relocation at address %x that "
- "had value %x that could not be "
- "converted in the destination.\n",
- TargetAddress,
- TargetValue);
- }
- break;
- }
- //
- // Stick the old value (now an offset in the COFF image)
- // into the relocation's spot.
- //
- *(UINT32 *)TargetPointer = TargetValue;
- Result = ElfconvCoffAddFixup(
- Context,
- TargetAddress,
- EFI_IMAGE_REL_BASED_HIGHLOW);
- if (Result == FALSE) {
- return FALSE;
- }
- break;
- default:
- fprintf(stderr,
- "Bad 386 dynamic relocation type %d, offset "
- "%x, program header index %d.\n",
- ELF32_R_TYPE(Relocation->r_info),
- (UINTN)Relocation - (UINTN)ElfHeader,
- SectionIndex);
- return FALSE;
- }
- } else if (ElfHeader->e_machine == EM_ARM) {
- switch (ELF32_R_TYPE(Relocation->r_info)) {
- case R_ARM_RBASE:
- break;
- //
- // Relative relocations contain a default VA in them. First
- // convert that VA into a COFF offset, then create a
- // relocation for it. If the value conversion failed, it's
- // probably a relocation off in a stabs section, so ignore
- // it.
- //
- case R_ARM_RELATIVE:
- TargetAddress = Relocation->r_offset;
- Result = ElfconvConvertElfAddress(Context,
- &TargetAddress);
- if (Result == FALSE) {
- fprintf(stderr,
- "R_ARM_RELATIVE target convert failed.\n");
- return FALSE;
- }
- TargetPointer = Context->CoffFile + TargetAddress;
- TargetValue = *(UINT32 *)TargetPointer;
- Result = ElfconvConvertElfAddress(Context,
- &TargetValue);
- if (Result == FALSE) {
- if ((Context->Flags &
- ELFCONV_OPTION_VERBOSE) != 0) {
- printf("Skipping relocation at address %x that "
- "had value %x that could not be "
- "converted in the destination.\n",
- TargetAddress,
- TargetValue);
- }
- break;
- }
- //
- // Stick the old value (now an offset in the COFF image)
- // into the relocation's spot.
- //
- *(UINT32 *)TargetPointer = TargetValue;
- Result = ElfconvCoffAddFixup(
- Context,
- TargetAddress,
- EFI_IMAGE_REL_BASED_HIGHLOW);
- if (Result == FALSE) {
- return FALSE;
- }
- break;
- default:
- fprintf(stderr,
- "Bad ARM dynamic relocation type %d, offset "
- "%x, program header index %d.\n",
- ELF32_R_TYPE(Relocation->r_info),
- (UINTN)Relocation - (UINTN)ElfHeader,
- SectionIndex);
- return FALSE;
- }
- } else {
- assert(FALSE);
- return FALSE;
- }
- }
- }
- }
- //
- // Pad the page entry out by adding extra entries.
- //
- while ((Context->CoffOffset & (ELFCONV_COFF_ALIGNMENT - 1)) != 0) {
- ElfconvCoffAddFixupEntry(Context, 0);
- }
- //
- // Create the relocation section.
- //
- NtHeader = (EFI_IMAGE_OPTIONAL_HEADER_UNION *)(Context->CoffFile +
- Context->NtHeaderOffset);
- Directories = &(NtHeader->Pe32.OptionalHeader.DataDirectory[0]);
- Directory = &(Directories[EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC]);
- Directory->Size = Context->CoffOffset - Context->RelocationOffset;
- if (Directory->Size == 0) {
- Directory->VirtualAddress = 0;
- NtHeader->Pe32.FileHeader.NumberOfSections -= 1;
- } else {
- Directory->VirtualAddress = Context->RelocationOffset;
- Flags = EFI_IMAGE_SCN_CNT_INITIALIZED_DATA |
- EFI_IMAGE_SCN_MEM_DISCARDABLE |
- EFI_IMAGE_SCN_MEM_READ;
- ElfconvCreateSectionHeader(
- Context,
- ".reloc",
- Context->RelocationOffset,
- Context->CoffOffset - Context->RelocationOffset,
- Flags);
- }
- return TRUE;
- }
- BOOLEAN
- ElfconvWriteDebug32 (
- PELFCONV_CONTEXT Context
- )
- /*++
- Routine Description:
- This routine writes out the debug sections.
- Arguments:
- Context - Supplies a pointer to the ELF context.
- Return Value:
- TRUE on success.
- FALSE on failure.
- --*/
- {
- Elf32_Ehdr *ElfHeader;
- Elf32_Shdr *ElfSection;
- UINT32 Flags;
- VOID *NewBuffer;
- EFI_IMAGE_OPTIONAL_HEADER_UNION *NtHeader;
- UINT32 SectionCount;
- UINTN SectionIndex;
- CHAR8 *SectionName;
- UINT32 SectionOffset;
- Elf32_Shdr *StringSection;
- ElfHeader = Context->InputFile;
- assert((ElfHeader->e_machine == EM_386) ||
- (ElfHeader->e_machine == EM_ARM));
- //
- // Find and wrangle debug sections.
- //
- Context->DataOffset = Context->CoffOffset;
- SectionCount = 0;
- for (SectionIndex = 0;
- SectionIndex < ElfHeader->e_shnum;
- SectionIndex += 1) {
- ElfSection = ELFCONV_ELF_SECTION(ElfHeader, SectionIndex);
- if (ElfconvIsDebugSection(ElfHeader, ElfSection) == FALSE) {
- continue;
- }
- SectionOffset = Context->CoffOffset;
- Context->CoffSectionsOffset[SectionIndex] = SectionOffset;
- Context->CoffOffset += ElfSection->sh_size;
- NewBuffer = realloc(Context->CoffFile, Context->CoffOffset);
- if (NewBuffer == NULL) {
- return FALSE;
- }
- Context->CoffFile = NewBuffer;
- //
- // Create the section.
- //
- StringSection = ELFCONV_ELF_SECTION(ElfHeader, ElfHeader->e_shstrndx);
- SectionName = (CHAR8 *)ElfHeader + StringSection->sh_offset +
- ElfSection->sh_name;
- Flags = EFI_IMAGE_SCN_MEM_READ | EFI_IMAGE_SCN_MEM_DISCARDABLE;
- ElfconvCreateSectionHeader(Context,
- SectionName,
- SectionOffset,
- ElfSection->sh_size,
- Flags);
- memcpy(Context->CoffFile + SectionOffset,
- (UINT8 *)ElfHeader + ElfSection->sh_offset,
- ElfSection->sh_size);
- SectionCount += 1;
- }
- //
- // Also write out the string table at this point.
- //
- if (Context->StringTable != NULL) {
- NewBuffer = realloc(Context->CoffFile,
- Context->CoffOffset + Context->StringTableSize);
- if (NewBuffer == NULL) {
- return FALSE;
- }
- Context->CoffFile = NewBuffer;
- //
- // Set the final size in the string table, then copy the string table
- // to the image.
- //
- memcpy(Context->StringTable,
- &(Context->StringTableSize),
- sizeof(UINT32));
- memcpy(Context->CoffFile + Context->CoffOffset,
- Context->StringTable,
- Context->StringTableSize);
- NtHeader = (EFI_IMAGE_OPTIONAL_HEADER_UNION *)(Context->CoffFile +
- Context->NtHeaderOffset);
- NtHeader->Pe32.FileHeader.PointerToSymbolTable = Context->CoffOffset;
- Context->CoffOffset += Context->StringTableSize;
- }
- return TRUE;
- }
- VOID
- ElfconvSetImageSize32 (
- PELFCONV_CONTEXT Context
- )
- /*++
- Routine Description:
- This routine sets the final image size.
- Arguments:
- Context - Supplies a pointer to the conversion context.
- Return Value:
- None.
- --*/
- {
- EFI_IMAGE_OPTIONAL_HEADER_UNION *NtHeader;
- NtHeader = (EFI_IMAGE_OPTIONAL_HEADER_UNION *)(Context->CoffFile +
- Context->NtHeaderOffset);
- NtHeader->Pe32.OptionalHeader.SizeOfImage = Context->CoffOffset;
- return;
- }
- VOID
- ElfconvCleanUp32 (
- PELFCONV_CONTEXT Context
- )
- /*++
- Routine Description:
- This routine performs final cleanup actions.
- Arguments:
- Context - Supplies a pointer to the conversion context.
- Return Value:
- None.
- --*/
- {
- if (Context->CoffSectionsOffset != NULL) {
- free(Context->CoffSectionsOffset);
- Context->CoffSectionsOffset = NULL;
- }
- return;
- }
- BOOLEAN
- ElfconvIsTextSection (
- Elf32_Ehdr *ElfHeader,
- Elf32_Shdr *SectionHeader
- )
- /*++
- Routine Description:
- This routine determines if the given section is a text section.
- Arguments:
- ElfHeader - Supplies a pointer to the ELF file header.
- SectionHeader - Supplies a pointer to the section header in question.
- Return Value:
- TRUE if it is a text section.
- FALSE if it is not a text section.
- --*/
- {
- if ((SectionHeader->sh_type != SHT_PROGBITS) &&
- (SectionHeader->sh_type != SHT_REL)) {
- return FALSE;
- }
- if ((SectionHeader->sh_flags & (SHF_WRITE | SHF_ALLOC)) == SHF_ALLOC) {
- return TRUE;
- }
- return FALSE;
- }
- BOOLEAN
- ElfconvIsDataSection (
- Elf32_Ehdr *ElfHeader,
- Elf32_Shdr *SectionHeader
- )
- /*++
- Routine Description:
- This routine determines if the given section is a data section.
- Arguments:
- ElfHeader - Supplies a pointer to the ELF file header.
- SectionHeader - Supplies a pointer to the section header in question.
- Return Value:
- TRUE if it is a data section.
- FALSE if it is not a data section.
- --*/
- {
- if (ElfconvIsHiiRsrcSection(ElfHeader, SectionHeader) != FALSE) {
- return FALSE;
- }
- if ((SectionHeader->sh_type != SHT_PROGBITS) &&
- (SectionHeader->sh_type != SHT_NOBITS) &&
- (SectionHeader->sh_type != SHT_DYNAMIC)) {
- return FALSE;
- }
- if ((SectionHeader->sh_flags & (SHF_WRITE | SHF_ALLOC)) ==
- (SHF_WRITE | SHF_ALLOC)) {
- return TRUE;
- }
- return FALSE;
- }
- BOOLEAN
- ElfconvIsHiiRsrcSection (
- Elf32_Ehdr *ElfHeader,
- Elf32_Shdr *SectionHeader
- )
- /*++
- Routine Description:
- This routine determines if the given section is a HII .rsrc section.
- Arguments:
- ElfHeader - Supplies a pointer to the ELF file header.
- SectionHeader - Supplies a pointer to the section header in question.
- Return Value:
- TRUE if it is a HII rsrc section.
- FALSE if it is not the HII rsrc section.
- --*/
- {
- CHAR8 *SectionName;
- Elf32_Shdr *StringSection;
- StringSection = ELFCONV_ELF_SECTION(ElfHeader, ElfHeader->e_shstrndx);
- SectionName = (CHAR8 *)ElfHeader + StringSection->sh_offset +
- SectionHeader->sh_name;
- if (strcmp(SectionName, ELFCONV_HII_SECTION_NAME) == 0) {
- return TRUE;
- }
- return FALSE;
- }
- BOOLEAN
- ElfconvIsDebugSection (
- Elf32_Ehdr *ElfHeader,
- Elf32_Shdr *SectionHeader
- )
- /*++
- Routine Description:
- This routine determines if the given section is a stabs or stabs string
- section.
- Arguments:
- ElfHeader - Supplies a pointer to the ELF file header.
- SectionHeader - Supplies a pointer to the section header in question.
- Return Value:
- TRUE if it is a HII rsrc section.
- FALSE if it is not the HII rsrc section.
- --*/
- {
- CHAR8 **DebugSection;
- CHAR8 *SectionName;
- Elf32_Shdr *StringSection;
- StringSection = ELFCONV_ELF_SECTION(ElfHeader, ElfHeader->e_shstrndx);
- SectionName = (CHAR8 *)ElfHeader + StringSection->sh_offset +
- SectionHeader->sh_name;
- DebugSection = ElfconvDebugSections;
- while (*DebugSection != NULL) {
- if (strcmp(SectionName, *DebugSection) == 0) {
- return TRUE;
- }
- DebugSection += 1;
- }
- return FALSE;
- }
- VOID
- ElfconvThumbMovtImmediatePatch (
- UINT16 *Instruction,
- UINT16 Address
- )
- /*++
- Routine Description:
- This routine updates an ARM MOVT or MOVW immediate instruction.
- Arguments:
- Instruction - Supplies a pointer to the ARM MOVT or MOVW immediate
- instruction.
- Address - Supplies the new address to patch into the instruction.
- Return Value:
- None.
- --*/
- {
- UINT16 Patch;
- //
- // Patch the first 16-bit chunk of the instruction.
- //
- Patch = (Address >> 12) & 0x000F;
- if ((Address & (1 << 11)) != 0) {
- Patch |= 1 << 10;
- }
- *Instruction = (*Instruction & ~0x040F) | Patch;
- //
- // Patch the second 16-bit chunk of the instruction.
- //
- Patch = Address & 0x000000FF;
- Patch |= (Address << 4) & 0x00007000;
- Instruction += 1;
- *Instruction = (*Instruction & 0x70FF) | Patch;
- return;
- }
- BOOLEAN
- ElfconvConvertElfAddress (
- PELFCONV_CONTEXT Context,
- UINT32 *Address
- )
- /*++
- Routine Description:
- This routine converts a memory address in the ELF image into an offset
- within the COFF file.
- Arguments:
- Context - Supplies a pointer to the conversion context.
- Address - Supplies a pointer that on input contains the address to convert.
- On output, this will contain the COFF offset.
- Return Value:
- TRUE on success.
- FALSE if the address is not valid.
- --*/
- {
- Elf32_Ehdr *ElfHeader;
- Elf32_Shdr *SectionHeader;
- UINT32 SectionIndex;
- ElfHeader = Context->InputFile;
- //
- // Loop through all the sections looking for the one that contains this
- // address.
- //
- for (SectionIndex = 0;
- SectionIndex < ElfHeader->e_shnum;
- SectionIndex += 1) {
- SectionHeader = ELFCONV_ELF_SECTION(ElfHeader, SectionIndex);
- //
- // If the section header contains the address, then return the
- // equivalent offset in the COFF section.
- //
- if ((SectionHeader->sh_addr <= *Address) &&
- (SectionHeader->sh_addr + SectionHeader->sh_size > *Address)) {
- if (Context->CoffSectionsOffset[SectionIndex] == 0) {
- return FALSE;
- }
- *Address = *Address - SectionHeader->sh_addr +
- Context->CoffSectionsOffset[SectionIndex];
- return TRUE;
- }
- }
- return FALSE;
- }
|