From 2ab7f6b10d425a68bd327ab2fa7e9f2bfbc01924 Mon Sep 17 00:00:00 2001 From: SangYoun Kwak Date: Mon, 27 May 2024 15:29:02 +0900 Subject: [PATCH] Modify postlinker source for the readability For the readability of postlinker.c and postlinker_64.c, indentations and arrangements are modified. Change-Id: Ic8073bc29bc4c4626fad3703ab786d2af987efd5 Signed-off-by: SangYoun Kwak --- script/postlinker.c | 255 ++++++++++++++++++++---------------------- script/postlinker_64.c | 297 ++++++++++++++++++++++++------------------------- 2 files changed, 266 insertions(+), 286 deletions(-) diff --git a/script/postlinker.c b/script/postlinker.c index 6e1531b..99fdc5e 100755 --- a/script/postlinker.c +++ b/script/postlinker.c @@ -7,71 +7,63 @@ typedef unsigned short uint16_t; typedef unsigned int uint32_t; -typedef int int32_t; +typedef int int32_t; typedef uint16_t Elf32_Half; typedef uint32_t Elf32_Word; -typedef int32_t Elf32_Sword; +typedef int32_t Elf32_Sword; typedef uint32_t Elf32_Addr; typedef uint32_t Elf32_Off; typedef uint16_t Elf32_Section; #define EI_NIDENT (16) -typedef struct -{ - unsigned char e_ident[EI_NIDENT]; /* Magic number and other info */ - Elf32_Half e_type; /* Object file type */ - Elf32_Half e_machine; /* Architecture */ - Elf32_Word e_version; /* Object file version */ - Elf32_Addr e_entry; /* Entry point virtual address */ - Elf32_Off e_phoff; /* Program header table file offset */ - Elf32_Off e_shoff; /* Section header table file offset */ - Elf32_Word e_flags; /* Processor-specific flags */ - Elf32_Half e_ehsize; /* ELF header size in bytes */ - Elf32_Half e_phentsize; /* Program header table entry size */ - Elf32_Half e_phnum; /* Program header table entry count */ - Elf32_Half e_shentsize; /* Section header table entry size */ - Elf32_Half e_shnum; /* Section header table entry count */ - Elf32_Half e_shstrndx; /* Section header string table index */ +typedef struct { + unsigned char e_ident[EI_NIDENT]; /* Magic number and other info */ + Elf32_Half e_type; /* Object file type */ + Elf32_Half e_machine; /* Architecture */ + Elf32_Word e_version; /* Object file version */ + Elf32_Addr e_entry; /* Entry point virtual address */ + Elf32_Off e_phoff; /* Program header table file offset */ + Elf32_Off e_shoff; /* Section header table file offset */ + Elf32_Word e_flags; /* Processor-specific flags */ + Elf32_Half e_ehsize; /* ELF header size in bytes */ + Elf32_Half e_phentsize; /* Program header table entry size */ + Elf32_Half e_phnum; /* Program header table entry count */ + Elf32_Half e_shentsize; /* Section header table entry size */ + Elf32_Half e_shnum; /* Section header table entry count */ + Elf32_Half e_shstrndx; /* Section header string table index */ } Elf32_Ehdr; -/* Program segment header. */ - -typedef struct -{ - Elf32_Word p_type; /* Segment type */ - Elf32_Off p_offset; /* Segment file offset */ - Elf32_Addr p_vaddr; /* Segment virtual address */ - Elf32_Addr p_paddr; /* Segment physical address */ - Elf32_Word p_filesz; /* Segment size in file */ - Elf32_Word p_memsz; /* Segment size in memory */ - Elf32_Word p_flags; /* Segment flags */ - Elf32_Word p_align; /* Segment alignment */ +/* Program segment header */ +typedef struct { + Elf32_Word p_type; /* Segment type */ + Elf32_Off p_offset; /* Segment file offset */ + Elf32_Addr p_vaddr; /* Segment virtual address */ + Elf32_Addr p_paddr; /* Segment physical address */ + Elf32_Word p_filesz; /* Segment size in file */ + Elf32_Word p_memsz; /* Segment size in memory */ + Elf32_Word p_flags; /* Segment flags */ + Elf32_Word p_align; /* Segment alignment */ } Elf32_Phdr; -/* Dynamic section entry. */ - -typedef struct -{ - Elf32_Sword d_tag; /* Dynamic entry type */ - union - { - Elf32_Word d_val; /* Integer value */ - Elf32_Addr d_ptr; /* Address value */ - } d_un; +/* Dynamic section entry */ +typedef struct { + Elf32_Sword d_tag; /* Dynamic entry type */ + union { + Elf32_Word d_val; /* Integer value */ + Elf32_Addr d_ptr; /* Address value */ + } d_un; } Elf32_Dyn; -/* Symbol table entry. */ - -typedef struct -{ - Elf32_Word st_name; /* Symbol name (string tbl index) */ - Elf32_Addr st_value; /* Symbol value */ - Elf32_Word st_size; /* Symbol size */ - unsigned char st_info; /* Symbol type and binding */ - unsigned char st_other; /* No defined meaning, 0 */ - Elf32_Section st_shndx; /* Section index */ +/* Symbol table entry */ +typedef struct { + Elf32_Word st_name; /* Symbol name (string tbl index) */ + Elf32_Addr st_value; /* Symbol value */ + Elf32_Word st_size; /* Symbol size */ + unsigned char st_info; /* Symbol type and binding */ + unsigned char st_other; /* No defined meaning, 0 */ + Elf32_Section st_shndx; /* Section index */ } Elf32_Sym; #define PT_DYNAMIC 2 /* Dynamic linking information */ @@ -81,86 +73,72 @@ typedef struct #define DT_SYMTAB 6 /* Address of symbol table */ #define DT_SYMENT 11 /* Size of one symbol table entry */ #define DT_GARBAGE 0xffffffff /* End of processor-specific */ -#define DT_GNU_HASH 0x6ffffef5 /* GNU-style hash table. */ +#define DT_GNU_HASH 0x6ffffef5 /* GNU-style hash table. */ -/* Special section index. */ +/* Special section index */ +#define SHN_UNDEF 0 /* No section, undefined symbol */ +#define STT_NOTYPE 0 /* Symbol type is unspecified */ +#define STT_OBJECT 1 /* Symbol is a data object */ +#define STT_FUNC 2 /* Symbol is a code object */ -#define SHN_UNDEF 0 /* No section, undefined symbol. */ -#define STT_NOTYPE 0 /* Symbol type is unspecified */ -#define STT_OBJECT 1 /* Symbol is a data object */ -#define STT_FUNC 2 /* Symbol is a code object */ - -int PostLinker(char* fileName); - -int main(int argc, char* argv[]) -{ - int i; - for(i=1; i\n", fileName); exit(EXIT_FAILURE); } - if(*(int*)(elfHeader.e_ident) != ELF_FORMAT) - { + if (*(int*)(elfHeader.e_ident) != ELF_FORMAT) { printf("error[%s] : no ELF format\n", fileName); exit(EXIT_FAILURE); } - pSegmentEntries = NULL; - DynEntries = NULL; + pSegmentEntries = NULL; + DynEntries = NULL; /* Memory Allocation for Segment and Section entries */ pSegmentEntries = (Elf32_Phdr *)malloc(elfHeader.e_phentsize * elfHeader.e_phnum); /* Get Segment Header Information using ELF Header */ fseek(fp, elfHeader.e_phoff, SEEK_SET); - if(!fread(pSegmentEntries, elfHeader.e_phentsize * elfHeader.e_phnum, 1, fp)) - printf("error[%s] : file read \n", fileName ); + if (!fread(pSegmentEntries, elfHeader.e_phentsize * elfHeader.e_phnum, 1, fp)) + printf("error[%s] : file read \n", fileName); /* Get Dynamic segment Information using Segment Header */ - for(i=0; i\n", fileName); exit(EXIT_FAILURE); } - if(!fread(DynEntries, pSegmentEntries[i].p_memsz, 1, fp)) - { + + if (!fread(DynEntries, pSegmentEntries[i].p_memsz, 1, fp)) { printf("error[%s] : file read \n", fileName); exit(EXIT_FAILURE); } @@ -169,42 +147,48 @@ int PostLinker(char* fileName) } } - for(i=0; DynEntries[i].d_tag != DT_NULL ; i++) - { - switch(DynEntries[i].d_tag) - { + for (int i = 0; DynEntries[i].d_tag != DT_NULL; ++i) { + switch (DynEntries[i].d_tag) { case DT_NEEDED: done = DT_GARBAGE; DynEntries[i].d_tag = DT_GARBAGE; break; - case DT_SYMTAB: symbolOff = DynEntries[i].d_un.d_val; break; - case DT_SYMENT: symEntrySize = DynEntries[i].d_un.d_val; break; - case DT_HASH: hashOff = DynEntries[i].d_un.d_val; break; - case DT_GNU_HASH: hashOff = DynEntries[i].d_un.d_val; break; - default: break; + case DT_SYMTAB: + symbolOff = DynEntries[i].d_un.d_val; + break; + case DT_SYMENT: + symEntrySize = DynEntries[i].d_un.d_val; + break; + case DT_HASH: + hashOff = DynEntries[i].d_un.d_val; + break; + case DT_GNU_HASH: + hashOff = DynEntries[i].d_un.d_val; + break; + default: + break; } } - fseek(fp, hashOff+4,SEEK_SET); - if(!fread(&totalSymbolCnt, sizeof(Elf32_Word), 1, fp)) + fseek(fp, hashOff + 4, SEEK_SET); + if (!fread(&totalSymbolCnt, sizeof(Elf32_Word), 1, fp)) printf("error[%s] : file read \n", fileName); pMapSymbolTable = (Elf32_Sym*)malloc(totalSymbolCnt * sizeof(Elf32_Sym)); - fseek(fp, symbolOff,SEEK_SET); - if(!fread(pMapSymbolTable, symEntrySize * totalSymbolCnt, 1, fp)) + fseek(fp, symbolOff, SEEK_SET); + if (!fread(pMapSymbolTable, symEntrySize * totalSymbolCnt, 1, fp)) printf("error[%s] : file read \n", fileName); done2 = 0; - for(i=1; i\n", fileName); exit(EXIT_FAILURE); } - fseek(fp, symbolOff,SEEK_SET); - if(!fwrite(pMapSymbolTable, symEntrySize * totalSymbolCnt, 1, fp)) - { + fseek(fp, symbolOff, SEEK_SET); + if (!fwrite(pMapSymbolTable, symEntrySize * totalSymbolCnt, 1, fp)) { printf("error[%s] : file write \n", fileName); exit(EXIT_FAILURE); } fclose(fp); - printf("Success : %s\n", fileName); + printf("Success : %s\n", fileName); + return 1; } + +int main(int argc, char **argv) +{ + for (int i = 1; i < argc; ++i) + PostLinker(argv[i]); + return 0; +} diff --git a/script/postlinker_64.c b/script/postlinker_64.c index d9e89e5..2df08ac 100755 --- a/script/postlinker_64.c +++ b/script/postlinker_64.c @@ -5,162 +5,153 @@ #include #define ELF_FORMAT 0x464c457f -#define DT_GARBAGE 0xffffffff /* End of processor-specific */ +#define DT_GARBAGE 0xffffffff /* End of processor-specific */ -int PostLinker64(char* fileName); - -int main(int argc, char* argv[]) +int PostLinker64(char *fileName) { - int i; - for(i=1; i\n", fileName); + exit(EXIT_FAILURE); + } + + if (*(int*)(elfHeader.e_ident) != ELF_FORMAT) { + printf("error[%s] : no ELF format\n", fileName); + exit(EXIT_FAILURE); + } + + if (elfHeader.e_ident[EI_CLASS] != 2) { + printf("error[%s] : EI_CLASS is not 64-bit format\n", fileName); + exit(EXIT_FAILURE); + } + + pSegmentEntries = NULL; + DynEntries = NULL; + + /* Memory Allocation for Segment and Section entries */ + pSegmentEntries = (Elf64_Phdr *)malloc(elfHeader.e_phentsize * elfHeader.e_phnum); + + /* Get Segment Header Information using ELF Header */ + fseek(fp, elfHeader.e_phoff, SEEK_SET); + if (!fread(pSegmentEntries, elfHeader.e_phentsize * elfHeader.e_phnum, 1, fp)) + printf("error[%s] : file read \n", fileName); + + /* Get Dynamic segment Information using Segment Header */ + for (int i = 0; i < elfHeader.e_phnum; ++i) { + if (pSegmentEntries[i].p_type == PT_DYNAMIC) { + fseek(fp, pSegmentEntries[i].p_offset, SEEK_SET); + DynEntries = (Elf64_Dyn*)malloc(pSegmentEntries[i].p_memsz); + if (DynEntries == NULL) { + printf("error[%s] : memory allocation \n", fileName); + exit(EXIT_FAILURE); + } + if (!fread(DynEntries, pSegmentEntries[i].p_memsz, 1, fp)) { + printf("error[%s] : file read \n", fileName); + exit(EXIT_FAILURE); + } + pos = i; + break; + } + } + + for (int i = 0; DynEntries[i].d_tag != DT_NULL; ++i) { + switch (DynEntries[i].d_tag) { + case DT_NEEDED: + done = DT_GARBAGE; + DynEntries[i].d_tag = DT_GARBAGE; + break; + case DT_SYMTAB: + symbolOff = DynEntries[i].d_un.d_val; + break; + case DT_SYMENT: + symEntrySize = DynEntries[i].d_un.d_val; + break; + case DT_HASH: + hashOff = DynEntries[i].d_un.d_val; + break; + case DT_GNU_HASH: + hashOff = DynEntries[i].d_un.d_val; + break; + default: + break; + } + } + + fseek(fp, hashOff + 4, SEEK_SET); + if (!fread(&totalSymbolCnt, sizeof(Elf64_Word), 1, fp)) + printf("error[%s] : file read \n", fileName); + + pMapSymbolTable = (Elf64_Sym*)malloc(totalSymbolCnt * sizeof(Elf64_Sym)); + fseek(fp, symbolOff, SEEK_SET); + if (!fread(pMapSymbolTable, symEntrySize * totalSymbolCnt, 1, fp)) + printf("error[%s] : file read \n", fileName); + + done2 = 0; + for (int i = 1; i < totalSymbolCnt; ++i) { + if (pMapSymbolTable[i].st_shndx == SHN_UNDEF && + pMapSymbolTable[i].st_value == 0 && + ((pMapSymbolTable[i].st_info & 0xF) == STT_FUNC || + (pMapSymbolTable[i].st_info & 0xF) == STT_OBJECT || + (pMapSymbolTable[i].st_info & 0xF) == STT_NOTYPE)) { + if (pMapSymbolTable[i].st_name == 0) + done2 = 0; + + pMapSymbolTable[i].st_name = 0; + pMapSymbolTable[i].st_value = 0; + pMapSymbolTable[i].st_size = 0; + pMapSymbolTable[i].st_info = 0; + pMapSymbolTable[i].st_shndx = 0; + done2 = 1; + } + } + + if (!done2) { + printf("Undefined symbols are already removed : %s\n", fileName); + return 1; + } + + fseek(fp, pSegmentEntries[pos].p_offset, SEEK_SET); + if (!fwrite(DynEntries, pSegmentEntries[pos].p_memsz, 1, fp)) { + printf("error[%s] : file write \n", fileName); + exit(EXIT_FAILURE); + } + + fseek(fp, symbolOff, SEEK_SET); + if (!fwrite(pMapSymbolTable, symEntrySize * totalSymbolCnt, 1, fp)) { + printf("error[%s] : file write \n", fileName); + exit(EXIT_FAILURE); + } + + fclose(fp); + printf("Success : %s\n", fileName); + return 1; } -int PostLinker64(char* fileName) +int main(int argc, char **argv) { - Elf64_Ehdr elfHeader; - Elf64_Phdr *pSegmentEntries; - Elf64_Dyn *DynEntries; - Elf64_Sym *pMapSymbolTable; - int totalSymbolCnt; - int i; - int pos; - - FILE *fp; - int done, done2; - int symbolOff = 0; - int symEntrySize = 0; - int hashOff = 0; - int Len; - - fp = NULL; - fp = fopen(fileName, "r+"); - - if (fp == NULL) - { - printf("error[%s] : file open\n", fileName); - exit(EXIT_FAILURE); - } - - /* Get ELF Header Information */ - if(!fread((void*)&elfHeader, sizeof(Elf64_Ehdr), 1, fp)) - { - printf("error[%s] : file read
\n", fileName); - exit(EXIT_FAILURE); - } - - if(*(int*)(elfHeader.e_ident) != ELF_FORMAT) - { - printf("error[%s] : no ELF format\n", fileName); - exit(EXIT_FAILURE); - } - - if (elfHeader.e_ident[EI_CLASS] != 2 ) - { - printf("error[%s] : EI_CLASS is not 64-bit format\n", fileName); - exit(EXIT_FAILURE); - } - - pSegmentEntries = NULL; - DynEntries = NULL; - - /* Memory Allocation for Segment and Section entries */ - pSegmentEntries = (Elf64_Phdr *)malloc(elfHeader.e_phentsize * elfHeader.e_phnum); - - /* Get Segment Header Information using ELF Header */ - fseek(fp, elfHeader.e_phoff, SEEK_SET); - if(!fread(pSegmentEntries, elfHeader.e_phentsize * elfHeader.e_phnum, 1, fp)) - printf("error[%s] : file read \n", fileName ); - - /* Get Dynamic segment Information using Segment Header */ - for(i=0; i\n", fileName); - exit(EXIT_FAILURE); - } - if(!fread(DynEntries, pSegmentEntries[i].p_memsz, 1, fp)) - { - printf("error[%s] : file read \n", fileName); - exit(EXIT_FAILURE); - } - pos = i; - break; - } - } - - for(i=0; DynEntries[i].d_tag != DT_NULL ; i++) - { - switch(DynEntries[i].d_tag) - { - case DT_NEEDED: - done = DT_GARBAGE; - DynEntries[i].d_tag = DT_GARBAGE; - break; - case DT_SYMTAB: symbolOff = DynEntries[i].d_un.d_val; break; - case DT_SYMENT: symEntrySize = DynEntries[i].d_un.d_val; break; - case DT_HASH: hashOff = DynEntries[i].d_un.d_val; break; - case DT_GNU_HASH: hashOff = DynEntries[i].d_un.d_val; break; - default: break; - } - } - - fseek(fp, hashOff+4,SEEK_SET); - if(!fread(&totalSymbolCnt, sizeof(Elf64_Word), 1, fp)) - printf("error[%s] : file read \n", fileName); - - pMapSymbolTable = (Elf64_Sym*)malloc(totalSymbolCnt * sizeof(Elf64_Sym)); - fseek(fp, symbolOff,SEEK_SET); - if(!fread(pMapSymbolTable, symEntrySize * totalSymbolCnt, 1, fp)) - printf("error[%s] : file read \n", fileName); - - done2 = 0; - for(i=1; i\n", fileName); - exit(EXIT_FAILURE); - } - - fseek(fp, symbolOff,SEEK_SET); - if(!fwrite(pMapSymbolTable, symEntrySize * totalSymbolCnt, 1, fp)) - { - printf("error[%s] : file write \n", fileName); - exit(EXIT_FAILURE); - } - - fclose(fp); - printf("Success : %s\n", fileName); - return 1; + for (int i = 1; i < argc; ++i) + PostLinker64(argv[i]); } -- 2.7.4