{"id":20976011,"url":"https://github.com/banyc/object_files","last_synced_at":"2026-04-27T13:34:47.901Z","repository":{"id":117847262,"uuid":"349915671","full_name":"Banyc/object_files","owner":"Banyc","description":"Experiment on object files","archived":false,"fork":false,"pushed_at":"2021-03-21T06:18:00.000Z","size":3,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-12-30T22:41:16.796Z","etag":null,"topics":["educational","linking","static-linking"],"latest_commit_sha":null,"homepage":"","language":"C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Banyc.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-03-21T06:17:54.000Z","updated_at":"2021-03-21T06:19:36.000Z","dependencies_parsed_at":null,"dependency_job_id":"a67510ca-c531-406b-ab93-63e30e66c3de","html_url":"https://github.com/Banyc/object_files","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/Banyc/object_files","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Banyc%2Fobject_files","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Banyc%2Fobject_files/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Banyc%2Fobject_files/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Banyc%2Fobject_files/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Banyc","download_url":"https://codeload.github.com/Banyc/object_files/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Banyc%2Fobject_files/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":32339289,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-26T23:26:28.701Z","status":"online","status_checked_at":"2026-04-27T02:00:06.769Z","response_time":128,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["educational","linking","static-linking"],"created_at":"2024-11-19T04:48:43.528Z","updated_at":"2026-04-27T13:34:47.878Z","avatar_url":"https://github.com/Banyc.png","language":"C","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Experiment on Object Files\n\nAn experiment on object files.\n\nBasically on static linking.\n\n## Steps\n\n1.  Go to WSL environment.\n1.  Move the terminal panel to the right.\n1.  Compile the code: `make main`.\n1.  experiments:\n    -   check disassembled:\n        -   `objdump -d main`\n        -   `objdump -d main.o`\n        -   `objdump -d sum.o`\n\n    -   check the symbols:\n        -   `readelf -SWs main`\n        -   `readelf -SWs main.o`\n        -   `readelf -SWs sum.o`\n    \n    -   check symbol table:\n        -   `readelf -r main`\n        -   `readelf -r main.o`\n        -   `readelf -r sum.o`\n\n    -   check hex:\n        -   `hexdump -C main`\n        -   `hexdump -C main.o`\n        -   `hexdump -C sum.o`\n1.  cases\n    1.  `objdump -d main.o`\n        -   ```text\n                8:   be 02 00 00 00          mov    $0x2,%esi\n                d:   bf 00 00 00 00          mov    $0x0,%edi\n               12:   e8 00 00 00 00          callq  17 \u003cmain+0x17\u003e\n            ```\n            -   `02 00 00 00` -\u003e 2\n                -   little-endian\n                -   left is low address.\n                -   `int` has 4 bytes\n                -   hex has 4 bits.\n            -   line `8`: load argument `2`.\n            -   line `d`: load argument `array`.\n                -   `00 00 00 00`\n                    -   unknown address to `array`.\n            -   line `12`: call function `sum`.\n                -   `00 00 00 00`\n                    -   unknown address to `sum`.\n    1.  `objdump -d main`\n        -   In `\u003cmain\u003e`:\n\n            ```text\n              40049f:       be 02 00 00 00          mov    $0x2,%esi\n              4004a4:       bf 30 10 60 00          mov    $0x601030,%edi\n              4004a9:       e8 0a 00 00 00          callq  4004b8 \u003csum\u003e\n            ```\n            -   those addresses are resolved.\n    1.  `readelf -r main.o`\n        -   ```text\n            Relocation section '.rela.text' at offset 0x498 contains 2 entries:\n              Offset          Info           Type           Sym. Value    Sym. Name + Addend\n            00000000000e  000d0000000a R_X86_64_32       0000000000000000 array + 0\n            000000000013  000f00000002 R_X86_64_PC32     0000000000000000 sum - 4\n            ```\n            -   `R_X86_64_32` := replace the `0`s to a absolute address\n            -   `R_X86_64_PC32` := replace the `0`s to a relative address to PC.\n    1.  `objdump -dx main.o`\n        -   ```text\n               8:   be 02 00 00 00          mov    $0x2,%esi\n               d:   bf 00 00 00 00          mov    $0x0,%edi\n                                    e: R_X86_64_32  array\n              12:   e8 00 00 00 00          callq  17 \u003cmain+0x17\u003e\n                                    13: R_X86_64_PC32       sum-0x4\n              17:   89 45 fc                mov    %eax,-0x4(%rbp)\n            ```\n            -   it shows the relocation records.\n                -   `e: R_X86_64_32  array`\n                -   `13: R_X86_64_PC32       sum-0x4`\n            -   `e:`: at `e` offset from the beginning of `main`, there has a reference to a memory location.\n                -   memory location: global data structure array.\n            -   `R_X86_64_32  array`\n                -   the target address, linker overwrites the `00 00 00 00` to which, is the absolute address of `array`.\n                -   `R_X86_64_32`\n                    -   `32`: 32 bits -\u003e 4 bytes -\u003e overwrite the following four bytes -\u003e overwrite `00 00 00 00`.\n            -   `R_X86_64_PC32       sum-0x4`\n                -   `-0x4`: `addend`.\n                -   `\u003cthe exact address of sum\u003e - 4 = \u003caddress of the 00 00 00 00\u003e + \u003caddress that is overwrited to 00 00 00 00\u003e`.\n                -   PC alway pointers to the next instruction while CPU is working on the previous instruction.\n                    -   PC is pointing `17` while the instruction at `12` is executing.\n    1.  `objdump -d main`\n        -   In `\u003cmain\u003e`:\n\n            ```text\n              40049f:       be 02 00 00 00          mov    $0x2,%esi\n              4004a4:       bf 30 10 60 00          mov    $0x601030,%edi\n              4004a9:       e8 0a 00 00 00          callq  4004b8 \u003csum\u003e\n              4004ae:       89 45 fc                mov    %eax,-0x4(%rbp)\n            ```\n            -   `0a 00 00 00` = `4004b8` - `4004ae`.\n            -   PC alway pointers to the next instruction while CPU is working on the previous instruction.\n                -   PC is pointing `4004ae` while the instruction at `4004a9` is executing.\n    1.  `objdump -dx main`\n        -   ```text\n            SYMBOL TABLE:\n            ...\n            0000000000601030 g     O .data  0000000000000010              array\n            ...\n            00000000004004b8 g     F .text  0000000000000045              sum\n            ```\n            -   linker at static linking\n                1.  linker combine the relocatable object files.\n                1.  linker looks for the relocation entries in `.rel.data` and `.rel.text` in `main.o` and `sum.o`.\n                    -   eg. linker found `array` in `main.o`. \n                1.  linker finds exact address of the `array` in the combined file.\n                1.  linker replaces the 4 bytes `00 00 00 00` started from address `e` to the new address.\n            -   `.data`: `array` is put at `.data` section.\n1.  compile `make main.2`\n1.  cases\n    1.  `objdump -dx main.2.o`\n        -   ```text\n               8:   be 02 00 00 00          mov    $0x2,%esi\n               d:   bf 00 00 00 00          mov    $0x0,%edi\n                                    e: R_X86_64_32  array+0xc\n              12:   e8 00 00 00 00          callq  17 \u003cmain+0x17\u003e\n                                    13: R_X86_64_PC32       sum-0x4\n            ```\n            -   `array+0xc`\n                -   `+0xc`\n                    -   `addend`.\n                    -   the array offset.\n\n## Additions\n\n### `readelf`\n\n-   `Ndx` := index of the section where it has been.\n    -   `UND` := unknown. From other relocatable object file.\n    -   `1` := `.text`\n    -   `3` := `.data`\n-   `.rela` := relocation entries.\n    -   only relocatable objects have.\n    -   executable objects do not have.\n        -   those entries are resolved by linking.\n\n### Relocation entries\n\n```c\ntypedef struct {\n    long offset;     /* Offset of the reference to relocate */\n    long type:32,    /* Relocation type */\n         symbol:32;  /* Symbol table index */\n    long addend;     /* Constant part of relocation expression */\n} Elf64_Rela;\n```\n-   `offset`\n    -   offset in `.text`/`.data` section.\n\n#### Example\n\n`objdump -dx main.o`:\n\n```text\n   8:   be 02 00 00 00          mov    $0x2,%esi\n   d:   bf 00 00 00 00          mov    $0x0,%edi\n                        e: R_X86_64_32  array\n  12:   e8 00 00 00 00          callq  17 \u003cmain+0x17\u003e\n                        13: R_X86_64_PC32       sum-0x4\n  17:   89 45 fc                mov    %eax,-0x4(%rbp)\n```\n\n-   `array`\n    -   `offset = e`\n    -   `type = R_X86_64_32`\n    -   `symbol` not shown here.\n    -   `addend = 0`\n-   `sum`\n    -   `offset = 13`\n    -   `type = R_X86_64_PC32`\n    -   `symbol` not shown here.\n    -   `addend = -0x4`\n\n### GCC\n\n-   `no-pie`\n    -   That flag is telling gcc not to make a position independent executable (PIE). PIE is a precodition to enable address space layout randomization (ASLR). ASLR is a security feature where the kernel loads the binary and dependencies into a random location of virtual memory each time it's run.\n\n### Tools\n\n-   GNU `binutils`\n    -   `ar`. Creates static libraries, and inserts, deletes, lists, and extracts members.\n    -   `strings`. Lists all of the printable strings contained in an object file.\n    -   `strip`. Deletes symbol table information from an object file.\n    -   `nm`. Lists the symbols defined in the symbol table of an object file. \n    -   `size`. Lists the names and sizes of the sections in an object file.\n    -   `readelf`. Displays the complete structure of an object file, including all of the information encoded in the ELF header. Subsumes the functionality of `size` and `nm`.\n    -   `objdump`. The mother of all binary tools. Can display all of the information in an object file. Its most useful function is disassembling the binary instructions in the `.text` section.\n\n-   Linux systems\n    -   `ldd`: Lists the shared libraries that an executable needs at run time.\n\n## Reference\n\n-   \u003chttps://youtu.be/E804eTETaQs\u003e\n-   csapp/Chapter 7: Linking.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbanyc%2Fobject_files","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbanyc%2Fobject_files","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbanyc%2Fobject_files/lists"}