This repository was archived by the owner on Feb 8, 2024. It is now read-only.
forked from dlang/druntime
-
Notifications
You must be signed in to change notification settings - Fork 27
Revamp rt/sections_android.d #178
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
8bcc545
core.internal.elf.dl: Account for Android/Bionic special case
kinke 54b685d
core.internal.elf.dl: Add method SharedObject.getPath()
kinke 37668d5
core.internal.elf.dl: Add convenience factory SharedObject.thisExecut…
kinke 2ed67d1
core.internal.elf.io: Enable convenience method for iterating over na…
kinke ea2bdf0
Add some core.internal.elf unittests
kinke afbf0f1
Revamp rt.sections_android, switching to parsing the ELF headers
kinke e89f60f
rt.sections_android: Don't bother excluding static TLS data from GC s…
kinke 2e95bb5
Android: Fix __WORDSIZE for 64-bit targets
kinke ff89130
Android: Fix Elf_Symndx for 64-bit targets
kinke File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,6 +13,7 @@ module core.internal.elf.io; | |
|
|
||
| version (Posix): | ||
|
|
||
| import core.lifetime : move; | ||
| import core.sys.posix.fcntl; | ||
| import core.sys.posix.sys.mman; | ||
| import core.sys.posix.unistd; | ||
|
|
@@ -134,41 +135,70 @@ template ElfIO(Elf_Ehdr, Elf_Shdr, ubyte ELFCLASS) | |
| return true; | ||
| } | ||
|
|
||
| /** | ||
| * Returns a struct to iterate over the named sections. | ||
| * Examples: | ||
| * -------------------- | ||
| * foreach (index, name, sectionHeader; elfFile.namedSections) ... | ||
| * -------------------- | ||
| */ | ||
| NamedSections namedSections() const | ||
| { | ||
| return NamedSections(this); | ||
| } | ||
|
|
||
| /** | ||
| * Tries to find the header of the section with the specified name. | ||
| * Returns: True on success. | ||
| */ | ||
| bool findSectionHeaderByName(const(char)[] sectionName, out ElfSectionHeader header) const | ||
| { | ||
| const index = findSectionIndexByName(sectionName); | ||
| if (index == -1) | ||
| return false; | ||
| header = ElfSectionHeader(this, index); | ||
| return true; | ||
| foreach (index, name, sectionHeader; namedSections) | ||
| { | ||
| if (name == sectionName) | ||
| { | ||
| header = move(sectionHeader); | ||
| return true; | ||
| } | ||
| } | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Tries to find the index of the section with the specified name. | ||
| * Returns: -1 if not found, otherwise 0-based section index. | ||
| */ | ||
| size_t findSectionIndexByName(const(char)[] sectionName) const | ||
| /// Enables iterating over an ELF file's (named) sections. | ||
| struct NamedSections | ||
| { | ||
| @nogc nothrow: | ||
| private const(ElfFile)* file; | ||
|
|
||
| private this(ref const ElfFile file) | ||
| { | ||
| import core.stdc.string : strlen; | ||
| this.file = &file; | ||
| } | ||
|
|
||
| const stringSectionHeader = ElfSectionHeader(this, ehdr.e_shstrndx); | ||
| const stringSection = ElfSection(this, stringSectionHeader); | ||
| /// name: null-terminated | ||
| alias Callback = int delegate(size_t index, const(char)[] name, ElfSectionHeader sectionHeader); | ||
|
|
||
| foreach (i; 0 .. ehdr.e_shnum) | ||
| /// | ||
| int opApply(scope Callback dg) | ||
| { | ||
| const stringSectionHeader = ElfSectionHeader(*file, file.ehdr.e_shstrndx); | ||
| const stringSection = ElfSection(*file, stringSectionHeader); | ||
|
|
||
| foreach (i; 0 .. file.ehdr.e_shnum) | ||
| { | ||
| auto sectionHeader = ElfSectionHeader(this, i); | ||
| auto pCurrentName = cast(const(char)*) (stringSection.data.ptr + sectionHeader.sh_name); | ||
| auto currentName = pCurrentName[0 .. strlen(pCurrentName)]; | ||
| if (sectionName == currentName) | ||
| return i; | ||
| import core.stdc.string : strlen; | ||
|
|
||
| auto sectionHeader = ElfSectionHeader(*file, i); | ||
| auto sectionName = cast(const(char)*) (stringSection.data.ptr + sectionHeader.sh_name); | ||
| const nameLen = strlen(sectionName); | ||
|
|
||
| const r = dg(i, sectionName[0 .. nameLen], move(sectionHeader)); | ||
| if (r != 0) | ||
| return r; | ||
| } | ||
|
|
||
| // not found | ||
| return -1; | ||
| return 0; | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -285,3 +315,29 @@ private struct MMapRegion(T) | |
| private ubyte[] mappedRegion; | ||
| const(T)* data; | ||
| } | ||
|
|
||
| version (LinuxOrBSD) | ||
| unittest | ||
| { | ||
| import core.internal.elf.dl, core.stdc.stdio; | ||
|
|
||
| SharedObject exe = SharedObject.thisExecutable(); | ||
|
|
||
| ElfFile file; | ||
| bool success = ElfFile.open(exe.name.ptr, file); | ||
| assert(success, "cannot open ELF file"); | ||
|
|
||
| foreach (index, name, sectionHeader; file.namedSections) | ||
| { | ||
| printf("section %3d %-32s", cast(int) index, name.ptr); | ||
| if (const offset = sectionHeader.shdr.sh_addr) | ||
| { | ||
| auto beg = exe.baseAddress + offset; | ||
| printf("%p - %p\n", beg, beg + sectionHeader.shdr.sh_size); | ||
| } | ||
| else | ||
| { | ||
| printf("not mapped into memory\n"); | ||
| } | ||
| } | ||
| } | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. E.g.: |
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
E.g.: