mirror of https://github.com/Nonannet/pelfy.git
47 KiB
47 KiB
In [13]:
%load_ext autoreload %autoreload 2
The autoreload extension is already loaded. To reload it, use: %reload_ext autoreload
In [38]:
import pelfy import pandas as pd
In [41]:
elf = pelfy.open_elf_file('obj/test-mips-o3.o') display(elf.code_relocations) #display(elf.architecture) #display(elf) display(elf.sections)
index | symbol name | type | calculation |
---|---|---|---|
0 | _gp_disp | R_MIPS_HI16 | ((A + S) >> 16) & 0xFFFF |
1 | _gp_disp | R_MIPS_LO16 | (A + S) & 0xFFFF |
2 | result_float | R_MIPS_GOT16 | G + A |
3 | result_float | R_MIPS_TLS_DTPREL32 | S + A - TLS_DTV_OFFSET |
4 | _gp_disp | R_MIPS_HI16 | ((A + S) >> 16) & 0xFFFF |
5 | _gp_disp | R_MIPS_LO16 | (A + S) & 0xFFFF |
6 | result_float | R_MIPS_GOT16 | G + A |
7 | result_float | R_MIPS_TLS_DTPREL32 | S + A - TLS_DTV_OFFSET |
8 | _gp_disp | R_MIPS_HI16 | ((A + S) >> 16) & 0xFFFF |
9 | _gp_disp | R_MIPS_LO16 | (A + S) & 0xFFFF |
10 | read_float_ret | R_MIPS_GOT16 | G + A |
11 | result_float_float | R_MIPS_GOT16 | G + A |
12 | result_float_float | R_MIPS_TLS_DTPREL32 | S + A - TLS_DTV_OFFSET |
index | name | type | description |
---|---|---|---|
0 | SHT_NULL | Section header table entry unused | |
1 | .text | SHT_PROGBITS | Program data |
2 | .rel.text | SHT_REL | Relocation entries, no addends |
3 | .data | SHT_PROGBITS | Program data |
4 | .bss | SHT_NOBITS | Program space with no data (bss) |
5 | .reginfo | 0x70000006 | Application-specific |
6 | .MIPS.abiflags | 0x7000002a | Application-specific |
7 | .pdr | SHT_PROGBITS | Program data |
8 | .rel.pdr | SHT_REL | Relocation entries, no addends |
9 | .mdebug.abi32 | SHT_PROGBITS | Program data |
10 | .comment | SHT_PROGBITS | Program data |
11 | .note.GNU-stack | SHT_PROGBITS | Program data |
12 | .gnu.attributes | 0x6ffffff5 | Processor-specific |
13 | .symtab | SHT_SYMTAB | Symbol table |
14 | .strtab | SHT_STRTAB | String table |
15 | .shstrtab | SHT_STRTAB | String table |
In [47]:
len(elf.sections)
Out[47]:
16
In [57]:
s = list(elf.sections) s[0].file
Out[57]:
e_ident[EI_MAG] 2135247942 0x7F followed by ELF(45 4c 46) in ASCII; these four bytes constitute the magic number e_ident[EI_CLASS] 1 This byte is set to either 1 or 2 to signify 32- or 64-bit format, respectively e_ident[EI_DATA] 2 This byte is set to either 1 or 2 to signify little or big endianness, respectively This affects interpretation of multi-byte fields starting with offset 0x10 e_ident[EI_VERSION] 1 Set to 1 for the original and current version of ELF e_ident[EI_OSABI] 0 Identifies the target operating system ABI e_ident[EI_ABIVERSION] 0 Further specifies the ABI version e_ident[EI_PAD] 0 Reserved padding bytes Currently unused Should be filled with zeros and ignored when read e_type 1 Identifies object file type e_machine 8 Specifies target instruction set architecture e_version 1 Set to 1 for the original version of ELF e_entry 0 This is the memory address of the entry point from where the process starts executing This field is either 32 or 64 bits long, depending on the format defined earlier (byte 0x04) If the file doesn't have an associated entry point, then this holds zero e_phoff 0 Points to the start of the program header table It usually follows the file header immediately following this one, making the offset 0x34 or 0x40 for 32- and 64-bit ELF executables, respectively e_shoff 1140 Points to the start of the section header table e_flags 1879052295 Interpretation of this field depends on the target architecture e_ehsize 52 Contains the size of this header, normally 64 Bytes for 64-bit and 52 Bytes for 32-bit format e_phentsize 0 Contains the size of a program header table entry As explained below, this will typically be 0x20 (32 bit) or 0x38 (64 bit) e_phnum 0 Contains the number of entries in the program header table e_shentsize 40 Contains the size of a section header table entry As explained below, this will typically be 0x28 (32 bit) or 0x40 (64 bit) e_shnum 16 Contains the number of entries in the section header table e_shstrndx 15 Contains index of the section header table entry that contains the section names
In [22]:
elf.architecture
Out[22]:
'EM_MIPS'
In [21]:
elf.sections['.rel.text']
Out[21]:
index 2 name .rel.text type SHT_REL (Relocation entries, no addends) sh_name 27 An offset to a string in the .shstrtab section that represents the name of this section. sh_type 9 Identifies the type of this header. sh_flags 64 Identifies the attributes of the section. sh_addr 0 Virtual address of the section in memory, for sections that are loaded. sh_offset 860 Offset of the section in the file image. sh_size 104 Size in bytes of the section in the file image. May be 0. sh_link 13 Contains the section index of an associated section. This field is used for several purposes, depending on the type of section. sh_info 1 Contains extra information about the section. This field is used for several purposes, depending on the type of section. sh_addralign 4 Contains the required alignment of the section. This field must be a power of two. sh_entsize 8 Contains the size, in bytes, of each entry, for sections that contain fixed-size entries. Otherwise, this field contains zero.
In [31]:
elf.symbols
Out[31]:
index | name | info | size | stb | description |
---|---|---|---|---|---|
0 | STT_NOTYPE | 0 | STB_LOCAL | Symbol type is unspecified | |
1 | test.c | STT_FILE | 0 | STB_LOCAL | Symbol's name is file name |
2 | STT_SECTION | 0 | STB_LOCAL | Symbol associated with a section | |
3 | STT_SECTION | 0 | STB_LOCAL | Symbol associated with a section | |
4 | STT_SECTION | 0 | STB_LOCAL | Symbol associated with a section | |
5 | STT_SECTION | 0 | STB_LOCAL | Symbol associated with a section | |
6 | STT_SECTION | 0 | STB_LOCAL | Symbol associated with a section | |
7 | STT_SECTION | 0 | STB_LOCAL | Symbol associated with a section | |
8 | STT_SECTION | 0 | STB_LOCAL | Symbol associated with a section | |
9 | STT_SECTION | 0 | STB_LOCAL | Symbol associated with a section | |
10 | STT_SECTION | 0 | STB_LOCAL | Symbol associated with a section | |
11 | STT_SECTION | 0 | STB_LOCAL | Symbol associated with a section | |
12 | result_float | STT_FUNC | 12 | STB_GLOBAL | Symbol is a code object |
13 | result_float_float | STT_FUNC | 24 | STB_GLOBAL | Symbol is a code object |
14 | add_float_float | STT_FUNC | 24 | STB_GLOBAL | Symbol is a code object |
15 | _gp_disp | STT_OBJECT | 0 | STB_GLOBAL | Symbol is a data object |
16 | mul_float_float | STT_FUNC | 24 | STB_GLOBAL | Symbol is a code object |
17 | read_float | STT_FUNC | 28 | STB_GLOBAL | Symbol is a code object |
18 | read_float_ret | STT_OBJECT | 4 | STB_GLOBAL | Symbol is a data object |
In [33]:
elf.get_relocations()
Out[33]:
index | symbol name | type | calculation |
---|---|---|---|
0 | _gp_disp | R_MIPS_HI16 | ((A + S) >> 16) & 0xFFFF |
1 | _gp_disp | R_MIPS_LO16 | (A + S) & 0xFFFF |
2 | result_float | R_MIPS_GOT16 | G + A |
3 | result_float | R_MIPS_TLS_DTPREL32 | S + A - TLS_DTV_OFFSET |
4 | _gp_disp | R_MIPS_HI16 | ((A + S) >> 16) & 0xFFFF |
5 | _gp_disp | R_MIPS_LO16 | (A + S) & 0xFFFF |
6 | result_float | R_MIPS_GOT16 | G + A |
7 | result_float | R_MIPS_TLS_DTPREL32 | S + A - TLS_DTV_OFFSET |
8 | _gp_disp | R_MIPS_HI16 | ((A + S) >> 16) & 0xFFFF |
9 | _gp_disp | R_MIPS_LO16 | (A + S) & 0xFFFF |
10 | read_float_ret | R_MIPS_GOT16 | G + A |
11 | result_float_float | R_MIPS_GOT16 | G + A |
12 | result_float_float | R_MIPS_TLS_DTPREL32 | S + A - TLS_DTV_OFFSET |
0 | result_float | R_MIPS_32 | S + A |
1 | result_float_float | R_MIPS_32 | S + A |
2 | add_float_float | R_MIPS_32 | S + A |
3 | mul_float_float | R_MIPS_32 | S + A |
4 | read_float | R_MIPS_32 | S + A |
In [36]:
elf.get_relocations('.rel.text')[2]#['result_float']
Out[36]:
index 12 symbol result_float relocation type R_MIPS_GOT16 (G + A) r_offset 48 r_info 3081 r_addend 0
In [11]:
elf.symbols
Out[11]:
index | name | info | size | stb | description |
---|---|---|---|---|---|
0 | STT_NOTYPE | 0 | STB_LOCAL | Type is unspecified | |
1 | test.c | STT_FILE | 0 | STB_LOCAL | Represents a file name |
2 | STT_SECTION | 0 | STB_LOCAL | Associated with a section | |
3 | STT_SECTION | 0 | STB_LOCAL | Associated with a section | |
4 | STT_SECTION | 0 | STB_LOCAL | Associated with a section | |
5 | STT_SECTION | 0 | STB_LOCAL | Associated with a section | |
6 | STT_SECTION | 0 | STB_LOCAL | Associated with a section | |
7 | STT_SECTION | 0 | STB_LOCAL | Associated with a section | |
8 | STT_SECTION | 0 | STB_LOCAL | Associated with a section | |
9 | STT_SECTION | 0 | STB_LOCAL | Associated with a section | |
10 | STT_SECTION | 0 | STB_LOCAL | Associated with a section | |
11 | STT_SECTION | 0 | STB_LOCAL | Associated with a section | |
12 | read_float_ret | STT_OBJECT | 4 | STB_GLOBAL | Data object (variable, array, etc.) |
13 | result_float | STT_FUNC | 48 | STB_GLOBAL | Function or executable code |
14 | result_float_float | STT_FUNC | 68 | STB_GLOBAL | Function or executable code |
15 | add_float_float | STT_FUNC | 104 | STB_GLOBAL | Function or executable code |
16 | _gp_disp | STT_OBJECT | 0 | STB_GLOBAL | Data object (variable, array, etc.) |
17 | mul_float_float | STT_FUNC | 104 | STB_GLOBAL | Function or executable code |
18 | read_float | STT_FUNC | 100 | STB_GLOBAL | Function or executable code |
In [ ]:
In [42]:
elf.sections['.rel.text'].get_data_hex()
Out[42]:
'00 00 00 74 00 00 10 05 00 00 00 78 00 00 10 06 00 00 00 AC 00 00 0D 09 00 00 00 B4 00 00 0D 25 00 00 00 DC 00 00 10 05 00 00 00 E0 00 00 10 06 00 00 01 14 00 00 0D 09 00 00 01 1C 00 00 0D 25 00 00 01 44 00 00 10 05 00 00 01 48 00 00 10 06 00 00 01 68 00 00 0C 09 00 00 01 78 00 00 0E 09 00 00 01 80 00 00 0E 25'
In [32]:
elf.sections[1].get_data_hex()
Out[32]:
'80 B4 83 B0 00 AF 87 ED 01 0A D7 ED 01 7A FD EE E7 7A 17 EE 90 3A 18 46 0C 37 BD 46 5D F8 04 7B 70 47 80 B4 83 B0 00 AF 87 ED 01 0A C7 ED 00 0A D7 ED 01 7A FD EE E7 7A 17 EE 90 2A D7 ED 00 7A FD EE E7 7A 17 EE 90 3A 13 44 18 46 0C 37 BD 46 5D F8 04 7B 70 47 80 B5 82 B0 00 AF 87 ED 01 0A C7 ED 00 0A 97 ED 01 7A D7 ED 00 7A 77 EE 27 7A B0 EE 67 0A FF F7 FE FF 00 BF 08 37 BD 46 80 BD 80 B5 82 B0 00 AF 87 ED 01 0A C7 ED 00 0A 97 ED 01 7A D7 ED 00 7A 67 EE 27 7A B0 EE 67 0A FF F7 FE FF 00 BF 08 37 BD 46 80 BD 80 B5 82 B0 00 AF 87 ED 01 0A 06 4B 7B 44 D3 ED 00 7A F0 EE 67 0A 97 ED 01 0A FF F7 FE FF 00 BF 08 37 BD 46 80 BD 16 00 00 00'
In [ ]: