ImageTool: Remove ImageInfo global state

This commit is contained in:
Marvin Häuser 2023-04-17 10:07:51 +02:00 committed by Mikhail Krichanov
parent b2de62e37f
commit 8ed1fada2d
3 changed files with 74 additions and 72 deletions

View File

@ -19,8 +19,6 @@ typedef struct {
#define EFI_IMAGE_MACHINE_AARCH64 0xAA64 #define EFI_IMAGE_MACHINE_AARCH64 0xAA64
#endif #endif
extern image_tool_image_info_t mImageInfo;
static static
Elf_Shdr * Elf_Shdr *
GetShdrByIndex ( GetShdrByIndex (
@ -342,6 +340,7 @@ ProcessRelocSection (
static static
RETURN_STATUS RETURN_STATUS
SetRelocs ( SetRelocs (
OUT image_tool_image_info_t *ImageInfo,
IN const tool_elf_scan_context *Context IN const tool_elf_scan_context *Context
) )
{ {
@ -383,15 +382,15 @@ SetRelocs (
break; break;
case R_X86_64_RELATIVE: case R_X86_64_RELATIVE:
case R_X86_64_64: case R_X86_64_64:
mImageInfo.RelocInfo.Relocs[RelNum].Type = EFI_IMAGE_REL_BASED_DIR64; ImageInfo->RelocInfo.Relocs[RelNum].Type = EFI_IMAGE_REL_BASED_DIR64;
mImageInfo.RelocInfo.Relocs[RelNum].Target = (uint32_t)(Rel->r_offset - BaseAddress); ImageInfo->RelocInfo.Relocs[RelNum].Target = (uint32_t)(Rel->r_offset - BaseAddress);
++RelNum; ++RelNum;
break; break;
case R_X86_64_32: case R_X86_64_32:
mImageInfo.RelocInfo.Relocs[RelNum].Type = EFI_IMAGE_REL_BASED_HIGHLOW; ImageInfo->RelocInfo.Relocs[RelNum].Type = EFI_IMAGE_REL_BASED_HIGHLOW;
mImageInfo.RelocInfo.Relocs[RelNum].Target = (uint32_t)(Rel->r_offset - BaseAddress); ImageInfo->RelocInfo.Relocs[RelNum].Target = (uint32_t)(Rel->r_offset - BaseAddress);
++RelNum; ++RelNum;
break; break;
@ -418,7 +417,7 @@ SetRelocs (
// //
break; break;
default: default:
fprintf (stderr, "ImageTool: Unsupported ELF EM_X86_64 relocation 0x%llx in %s\n", ELF_R_TYPE(Rel->r_info), mImageInfo.DebugInfo.SymbolsPath); fprintf (stderr, "ImageTool: Unsupported ELF EM_X86_64 relocation 0x%llx in %s\n", ELF_R_TYPE(Rel->r_info), ImageInfo->DebugInfo.SymbolsPath);
return RETURN_UNSUPPORTED; return RETURN_UNSUPPORTED;
} }
} else if (Ehdr->e_machine == EM_AARCH64) { } else if (Ehdr->e_machine == EM_AARCH64) {
@ -447,20 +446,20 @@ SetRelocs (
break; break;
case R_AARCH64_ABS64: case R_AARCH64_ABS64:
case R_AARCH64_RELATIVE: case R_AARCH64_RELATIVE:
mImageInfo.RelocInfo.Relocs[RelNum].Type = EFI_IMAGE_REL_BASED_DIR64; ImageInfo->RelocInfo.Relocs[RelNum].Type = EFI_IMAGE_REL_BASED_DIR64;
mImageInfo.RelocInfo.Relocs[RelNum].Target = (uint32_t)(Rel->r_offset - BaseAddress); ImageInfo->RelocInfo.Relocs[RelNum].Target = (uint32_t)(Rel->r_offset - BaseAddress);
++RelNum; ++RelNum;
break; break;
case R_AARCH64_ABS32: case R_AARCH64_ABS32:
mImageInfo.RelocInfo.Relocs[RelNum].Type = EFI_IMAGE_REL_BASED_HIGHLOW; ImageInfo->RelocInfo.Relocs[RelNum].Type = EFI_IMAGE_REL_BASED_HIGHLOW;
mImageInfo.RelocInfo.Relocs[RelNum].Target = (uint32_t)(Rel->r_offset - BaseAddress); ImageInfo->RelocInfo.Relocs[RelNum].Target = (uint32_t)(Rel->r_offset - BaseAddress);
++RelNum; ++RelNum;
break; break;
default: default:
fprintf (stderr, "ImageTool: Unsupported ELF EM_AARCH64 relocation 0x%llx in %s\n", ELF_R_TYPE(Rel->r_info), mImageInfo.DebugInfo.SymbolsPath); fprintf (stderr, "ImageTool: Unsupported ELF EM_AARCH64 relocation 0x%llx in %s\n", ELF_R_TYPE(Rel->r_info), ImageInfo->DebugInfo.SymbolsPath);
return RETURN_UNSUPPORTED; return RETURN_UNSUPPORTED;
} }
} }
@ -471,8 +470,8 @@ SetRelocs (
break; break;
case R_386_32: case R_386_32:
mImageInfo.RelocInfo.Relocs[RelNum].Type = EFI_IMAGE_REL_BASED_HIGHLOW; ImageInfo->RelocInfo.Relocs[RelNum].Type = EFI_IMAGE_REL_BASED_HIGHLOW;
mImageInfo.RelocInfo.Relocs[RelNum].Target = (Rel->r_offset - BaseAddress); ImageInfo->RelocInfo.Relocs[RelNum].Target = (Rel->r_offset - BaseAddress);
++RelNum; ++RelNum;
break; break;
@ -480,7 +479,7 @@ SetRelocs (
case R_386_PC32: case R_386_PC32:
break; break;
default: default:
fprintf (stderr, "ImageTool: Unsupported ELF EM_386 relocation 0x%x in %s\n", ELF_R_TYPE(Rel->r_info), mImageInfo.DebugInfo.SymbolsPath); fprintf (stderr, "ImageTool: Unsupported ELF EM_386 relocation 0x%x in %s\n", ELF_R_TYPE(Rel->r_info), ImageInfo->DebugInfo.SymbolsPath);
return RETURN_UNSUPPORTED; return RETURN_UNSUPPORTED;
} }
} else if (Ehdr->e_machine == EM_ARM) { } else if (Ehdr->e_machine == EM_ARM) {
@ -524,13 +523,13 @@ SetRelocs (
break; break;
case R_ARM_ABS32: case R_ARM_ABS32:
mImageInfo.RelocInfo.Relocs[RelNum].Type = EFI_IMAGE_REL_BASED_HIGHLOW; ImageInfo->RelocInfo.Relocs[RelNum].Type = EFI_IMAGE_REL_BASED_HIGHLOW;
mImageInfo.RelocInfo.Relocs[RelNum].Target = (Rel->r_offset - BaseAddress); ImageInfo->RelocInfo.Relocs[RelNum].Target = (Rel->r_offset - BaseAddress);
++RelNum; ++RelNum;
break; break;
default: default:
fprintf (stderr, "ImageTool: Unsupported ELF EM_ARM relocation 0x%x in %s\n", ELF_R_TYPE(Rel->r_info), mImageInfo.DebugInfo.SymbolsPath); fprintf (stderr, "ImageTool: Unsupported ELF EM_ARM relocation 0x%x in %s\n", ELF_R_TYPE(Rel->r_info), ImageInfo->DebugInfo.SymbolsPath);
return RETURN_UNSUPPORTED; return RETURN_UNSUPPORTED;
} }
} }
@ -538,7 +537,7 @@ SetRelocs (
} }
} }
mImageInfo.RelocInfo.NumRelocs = RelNum; ImageInfo->RelocInfo.NumRelocs = RelNum;
return RETURN_SUCCESS; return RETURN_SUCCESS;
} }
@ -546,6 +545,7 @@ SetRelocs (
static static
RETURN_STATUS RETURN_STATUS
CreateIntermediate ( CreateIntermediate (
OUT image_tool_image_info_t *ImageInfo,
IN const tool_elf_scan_context *Context IN const tool_elf_scan_context *Context
) )
{ {
@ -619,21 +619,21 @@ CreateIntermediate (
continue; continue;
} }
++mImageInfo.SegmentInfo.NumSegments; ++ImageInfo->SegmentInfo.NumSegments;
} }
if (mImageInfo.SegmentInfo.NumSegments == 0) { if (ImageInfo->SegmentInfo.NumSegments == 0) {
fprintf (stderr, "ImageTool: No loadable sections\n"); fprintf (stderr, "ImageTool: No loadable sections\n");
return RETURN_VOLUME_CORRUPTED; return RETURN_VOLUME_CORRUPTED;
} }
Segments = calloc (1, sizeof (*Segments) * mImageInfo.SegmentInfo.NumSegments); Segments = calloc (1, sizeof (*Segments) * ImageInfo->SegmentInfo.NumSegments);
if (Segments == NULL) { if (Segments == NULL) {
fprintf (stderr, "ImageTool: Could not allocate memory for Segments\n"); fprintf (stderr, "ImageTool: Could not allocate memory for Segments\n");
return RETURN_OUT_OF_RESOURCES; return RETURN_OUT_OF_RESOURCES;
}; };
mImageInfo.SegmentInfo.Segments = Segments; ImageInfo->SegmentInfo.Segments = Segments;
if (NumRelocs != 0) { if (NumRelocs != 0) {
Relocs = calloc (1, sizeof (*Relocs) * NumRelocs); Relocs = calloc (1, sizeof (*Relocs) * NumRelocs);
@ -642,7 +642,7 @@ CreateIntermediate (
return RETURN_OUT_OF_RESOURCES; return RETURN_OUT_OF_RESOURCES;
}; };
mImageInfo.RelocInfo.Relocs = Relocs; ImageInfo->RelocInfo.Relocs = Relocs;
} }
for (Index = 0; Index < Ehdr->e_shnum; ++Index) { for (Index = 0; Index < Ehdr->e_shnum; ++Index) {
@ -658,19 +658,19 @@ CreateIntermediate (
} }
if (IsHiiRsrcShdr (Ehdr, Shdr)) { if (IsHiiRsrcShdr (Ehdr, Shdr)) {
mImageInfo.HiiInfo.DataSize = (uint32_t)Shdr->sh_size; ImageInfo->HiiInfo.DataSize = (uint32_t)Shdr->sh_size;
mImageInfo.HiiInfo.Data = calloc (1, mImageInfo.HiiInfo.DataSize); ImageInfo->HiiInfo.Data = calloc (1, ImageInfo->HiiInfo.DataSize);
if (mImageInfo.HiiInfo.Data == NULL) { if (ImageInfo->HiiInfo.Data == NULL) {
fprintf (stderr, "ImageTool: Could not allocate memory for Hii Data\n"); fprintf (stderr, "ImageTool: Could not allocate memory for Hii Data\n");
return RETURN_OUT_OF_RESOURCES; return RETURN_OUT_OF_RESOURCES;
}; };
if (Shdr->sh_type == SHT_PROGBITS) { if (Shdr->sh_type == SHT_PROGBITS) {
memcpy (mImageInfo.HiiInfo.Data, (const char *)Ehdr + Shdr->sh_offset, mImageInfo.HiiInfo.DataSize); memcpy (ImageInfo->HiiInfo.Data, (const char *)Ehdr + Shdr->sh_offset, ImageInfo->HiiInfo.DataSize);
} }
SetHiiResourceHeader (mImageInfo.HiiInfo.Data, (UINT32)(Shdr->sh_addr - BaseAddress)); SetHiiResourceHeader (ImageInfo->HiiInfo.Data, (UINT32)(Shdr->sh_addr - BaseAddress));
} else { } else {
Name = GetString (Ehdr, Shdr->sh_name, 0); Name = GetString (Ehdr, Shdr->sh_name, 0);
if (Name == NULL) { if (Name == NULL) {
@ -706,13 +706,14 @@ CreateIntermediate (
} }
} }
assert (SIndex == mImageInfo.SegmentInfo.NumSegments); assert (SIndex == ImageInfo->SegmentInfo.NumSegments);
return SetRelocs(Context); return SetRelocs (ImageInfo, Context);
} }
RETURN_STATUS RETURN_STATUS
ScanElf ( ScanElf (
OUT image_tool_image_info_t *ImageInfo,
IN const void *File, IN const void *File,
IN uint32_t FileSize, IN uint32_t FileSize,
IN const char *SymbolsPath IN const char *SymbolsPath
@ -723,6 +724,7 @@ ScanElf (
const Elf_Ehdr *Ehdr; const Elf_Ehdr *Ehdr;
Elf_Addr BaseAddress; Elf_Addr BaseAddress;
assert (ImageInfo != NULL);
assert (File != NULL || FileSize == 0); assert (File != NULL || FileSize == 0);
Status = ParseElfFile (&Context, File, FileSize); Status = ParseElfFile (&Context, File, FileSize);
@ -733,29 +735,29 @@ ScanElf (
Ehdr = Context.Ehdr; Ehdr = Context.Ehdr;
BaseAddress = Context.BaseAddress; BaseAddress = Context.BaseAddress;
memset (&mImageInfo, 0, sizeof (mImageInfo)); memset (ImageInfo, 0, sizeof (*ImageInfo));
mImageInfo.HeaderInfo.BaseAddress = BaseAddress; ImageInfo->HeaderInfo.BaseAddress = BaseAddress;
mImageInfo.HeaderInfo.EntryPointAddress = (uint32_t)(Ehdr->e_entry - BaseAddress); ImageInfo->HeaderInfo.EntryPointAddress = (uint32_t)(Ehdr->e_entry - BaseAddress);
mImageInfo.HeaderInfo.IsXip = true; ImageInfo->HeaderInfo.IsXip = true;
mImageInfo.SegmentInfo.SegmentAlignment = (uint32_t)Context.Alignment; ImageInfo->SegmentInfo.SegmentAlignment = (uint32_t)Context.Alignment;
mImageInfo.RelocInfo.RelocsStripped = false; ImageInfo->RelocInfo.RelocsStripped = false;
mImageInfo.DebugInfo.SymbolsPathLen = strlen (SymbolsPath); ImageInfo->DebugInfo.SymbolsPathLen = strlen (SymbolsPath);
switch (Ehdr->e_machine) { switch (Ehdr->e_machine) {
#if defined(EFI_TARGET64) #if defined(EFI_TARGET64)
case EM_X86_64: case EM_X86_64:
mImageInfo.HeaderInfo.Machine = EFI_IMAGE_MACHINE_X64; ImageInfo->HeaderInfo.Machine = EFI_IMAGE_MACHINE_X64;
break; break;
case EM_AARCH64: case EM_AARCH64:
mImageInfo.HeaderInfo.Machine = EFI_IMAGE_MACHINE_AARCH64; ImageInfo->HeaderInfo.Machine = EFI_IMAGE_MACHINE_AARCH64;
break; break;
#elif defined(EFI_TARGET32) #elif defined(EFI_TARGET32)
case EM_386: case EM_386:
mImageInfo.HeaderInfo.Machine = EFI_IMAGE_MACHINE_IA32; ImageInfo->HeaderInfo.Machine = EFI_IMAGE_MACHINE_IA32;
break; break;
case EM_ARM: case EM_ARM:
mImageInfo.HeaderInfo.Machine = EFI_IMAGE_MACHINE_ARMTHUMB_MIXED; ImageInfo->HeaderInfo.Machine = EFI_IMAGE_MACHINE_ARMTHUMB_MIXED;
break; break;
#endif #endif
default: default:
@ -763,22 +765,22 @@ ScanElf (
return RETURN_INCOMPATIBLE_VERSION; return RETURN_INCOMPATIBLE_VERSION;
} }
mImageInfo.DebugInfo.SymbolsPath = malloc (mImageInfo.DebugInfo.SymbolsPathLen + 1); ImageInfo->DebugInfo.SymbolsPath = malloc (ImageInfo->DebugInfo.SymbolsPathLen + 1);
if (mImageInfo.DebugInfo.SymbolsPath == NULL) { if (ImageInfo->DebugInfo.SymbolsPath == NULL) {
fprintf (stderr, "ImageTool: Could not allocate memory for Debug Data\n"); fprintf (stderr, "ImageTool: Could not allocate memory for Debug Data\n");
return RETURN_OUT_OF_RESOURCES; return RETURN_OUT_OF_RESOURCES;
}; };
memmove (mImageInfo.DebugInfo.SymbolsPath, SymbolsPath, mImageInfo.DebugInfo.SymbolsPathLen + 1); memmove (ImageInfo->DebugInfo.SymbolsPath, SymbolsPath, ImageInfo->DebugInfo.SymbolsPathLen + 1);
// //
// There is no corresponding ELF property. // There is no corresponding ELF property.
// //
mImageInfo.HeaderInfo.Subsystem = 0; ImageInfo->HeaderInfo.Subsystem = 0;
Status = CreateIntermediate (&Context); Status = CreateIntermediate (ImageInfo, &Context);
if (RETURN_ERROR (Status)) { if (RETURN_ERROR (Status)) {
ToolImageDestruct (&mImageInfo); ToolImageDestruct (ImageInfo);
} }
return Status; return Status;

View File

@ -11,8 +11,6 @@
#define EFI_ACPI_1_0_FIRMWARE_ACPI_CONTROL_STRUCTURE_VERSION 0x0 #define EFI_ACPI_1_0_FIRMWARE_ACPI_CONTROL_STRUCTURE_VERSION 0x0
image_tool_image_info_t mImageInfo;
static static
RETURN_STATUS RETURN_STATUS
@ -353,6 +351,7 @@ GenExecutable (
VOID *InputFile; VOID *InputFile;
RETURN_STATUS Status; RETURN_STATUS Status;
bool Result; bool Result;
image_tool_image_info_t ImageInfo;
void *OutputFile; void *OutputFile;
uint32_t OutputFileSize; uint32_t OutputFileSize;
@ -362,9 +361,9 @@ GenExecutable (
return RETURN_ABORTED; return RETURN_ABORTED;
} }
Status = ToolContextConstructPe (&mImageInfo, InputFile, InputFileSize); Status = ToolContextConstructPe (&ImageInfo, InputFile, InputFileSize);
if (Status == RETURN_UNSUPPORTED) { if (Status == RETURN_UNSUPPORTED) {
Status = ScanElf (InputFile, InputFileSize, InputFileName); Status = ScanElf (&ImageInfo, InputFile, InputFileSize, InputFileName);
} }
free (InputFile); free (InputFile);
@ -375,26 +374,26 @@ GenExecutable (
} }
if (TypeName != NULL) { if (TypeName != NULL) {
Result = ImageSetModuleType (&mImageInfo, TypeName); Result = ImageSetModuleType (&ImageInfo, TypeName);
if (!Result) { if (!Result) {
ToolImageDestruct (&mImageInfo); ToolImageDestruct (&ImageInfo);
return RETURN_UNSUPPORTED; return RETURN_UNSUPPORTED;
} }
} }
Result = CheckToolImage (&mImageInfo); Result = CheckToolImage (&ImageInfo);
if (!Result) { if (!Result) {
ToolImageDestruct (&mImageInfo); ToolImageDestruct (&ImageInfo);
return RETURN_UNSUPPORTED; return RETURN_UNSUPPORTED;
} }
if (strcmp (FormatName, "PE") == 0) { if (strcmp (FormatName, "PE") == 0) {
OutputFile = ToolImageEmitPe (&mImageInfo, &OutputFileSize); OutputFile = ToolImageEmitPe (&ImageInfo, &OutputFileSize);
} else { } else {
assert (false); assert (false);
} }
ToolImageDestruct (&mImageInfo); ToolImageDestruct (&ImageInfo);
if (OutputFile == NULL) { if (OutputFile == NULL) {
return RETURN_ABORTED; return RETURN_ABORTED;

View File

@ -163,6 +163,7 @@ ToolImageEmitPe (
RETURN_STATUS RETURN_STATUS
ScanElf ( ScanElf (
OUT image_tool_image_info_t *ImageInfo,
IN const void *File, IN const void *File,
IN uint32_t FileSize, IN uint32_t FileSize,
IN const char *SymbolsPath IN const char *SymbolsPath