{"id":15779477,"url":"https://github.com/jonatansh/py-elf-structs","last_synced_at":"2025-10-25T06:11:38.288Z","repository":{"id":62591148,"uuid":"484415386","full_name":"jonatanSh/py-elf-structs","owner":"jonatanSh","description":"Python library to extract struct and type information from elf and build python structs","archived":false,"fork":false,"pushed_at":"2023-05-25T09:55:15.000Z","size":49,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2024-10-05T18:06:14.424Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Python","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/jonatanSh.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":"2022-04-22T11:54:23.000Z","updated_at":"2024-03-15T18:27:41.000Z","dependencies_parsed_at":"2024-10-25T23:28:00.485Z","dependency_job_id":"421d06c0-8015-4761-a3d7-4a9f382585bc","html_url":"https://github.com/jonatanSh/py-elf-structs","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jonatanSh%2Fpy-elf-structs","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jonatanSh%2Fpy-elf-structs/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jonatanSh%2Fpy-elf-structs/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jonatanSh%2Fpy-elf-structs/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jonatanSh","download_url":"https://codeload.github.com/jonatanSh/py-elf-structs/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246492047,"owners_count":20786297,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","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":[],"created_at":"2024-10-04T18:06:27.973Z","updated_at":"2025-10-25T06:11:33.252Z","avatar_url":"https://github.com/jonatanSh.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Py-elf-structs\nThis repository parse dwarf information from elfs and generate python structs accordingly\n\n\n\n# Usage\nFirst lets write our elf:\n\n```c\nstruct command {\n    char command[64];\n};\n\nstruct command_with_args {\n    char arg1[128];\n    struct command command;\n};\n/*\n    Ignore this part it is only done for disabling optimization\n    Optimization will omit the structs if they are not being used \n    -O0 omits this structs from the output for some reason\n*/\nvoid main() {\n    struct command a = {};\n    struct command_with_args b = {};\n    printf(\"a = %p, b=%p\\n\", a, b);\n}\n\n```\n\nWhile compiling we must generate type information:\n\n```bash\ngcc main.c -dwarf-2 -ggdb -o a.out\n```\n\nThen generate python structs\n\n```python\npython -m py_elf_structs a.out /tmp/structs.json\n```\n\nFinally, load the structs and interact with them\n\n```python\nfrom py_elf_structs import load_structs\n\nstructs = load_structs(\"/tmp/structs.json\")\n\ncommand_with_args = structs.command_with_args(arg=\"/tmp\", \n                          command=structs.command(\n                              command=\"ls -la\"\n                          ))\n\n# You can pack this struct\ncommand_with_args.pack()\n\n# Unpack is also supported\ncommand_with_args = structs.command_with_args.unpack(\"\u003cstream\u003e\")\n```\n\nYou can also use a python api to generate the structs.json file:\n```python\nfrom py_elf_structs import generate_structs\nsrc_file=\"a.out\"\noutput_file=\"/tmp/structs.json\"\nverbose=True\ngenerate_structs(src_file=src_file,\n                 output_file=output_file,\n                 is_verbose=verbose)\n```\n\n# Protected attributes\nAttribute with the name size is used by the parser therefor if a struct contain a variable named\nsize it is replaced by _size\neg ..\n```c\nstruct my_struct {\n    int size;\n}\n```\npython api:\n```python\nfrom py_elf_structs import load_structs\nstructs = load_structs(\"/tmp/structs.json\")\nstructs.my_struct(_size=2)\n```\n\n# Struct alignment\nStruct maybe aligned to sizeof(ptr) therefore we should support this\neg ...\n```c\nstruct command {\n    unsigned int address;\n    unsigned short value;\n};\n```\nThe resulting cstruct is:\n```c\nstruct command {\n    unsigned int address;\n    unsigned short value[2];\n};\n```\nBecause this struct is aligned to 4 it is handled by the api and you can create this struct anyway:\n```python\nfrom py_elf_structs import load_structs\n\nstructs = load_structs(\"/tmp/structs.json\")\n\nstructs.command(address=1, value=2)\n# This will create the struct and fix value to be an array\n```\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjonatansh%2Fpy-elf-structs","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjonatansh%2Fpy-elf-structs","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjonatansh%2Fpy-elf-structs/lists"}