Skip to content
This repository has been archived by the owner on Oct 12, 2022. It is now read-only.

Refactor ELF utils from rt.backtrace.elf and rt.sections_elf_shared to core.internal.elf.{dl,io} #2330

Merged
merged 16 commits into from Feb 21, 2020

Conversation

kinke
Copy link
Contributor

@kinke kinke commented Oct 14, 2018

A common place for working with ELF binaries, accessible by user code as well and providing a bit more convenience.

@dlang-bot
Copy link
Contributor

dlang-bot commented Oct 14, 2018

Thanks for your pull request and interest in making D better, @kinke! We are looking forward to reviewing it, and you should be hearing from a maintainer soon.
Please verify that your PR follows this checklist:

  • My PR is fully covered with tests (you can see the coverage diff by visiting the details link of the codecov check)
  • My PR is as minimal as possible (smaller, focused PRs are easier to review than big ones)
  • I have provided a detailed rationale explaining my changes
  • New or modified functions have Ddoc comments (with Params: and Returns:)

Please see CONTRIBUTING.md for more information.


If you have addressed all reviews or aren't sure how to proceed, don't hesitate to ping us with a simple comment.

Bugzilla references

Auto-close Bugzilla Severity Description
19322 regression A lot of memory is consumed and not freed to the system when Exception is formatted with stacktrace in debug

⚠️⚠️⚠️ Warnings ⚠️⚠️⚠️

To target stable perform these two steps:

  1. Rebase your branch to upstream/stable:
git rebase --onto upstream/stable upstream/master
  1. Change the base branch of your PR to stable

Testing this PR locally

If you don't have a local development environment setup, you can use Digger to test this PR:

dub fetch digger
dub run digger -- build "master + druntime#2330"

Copy link
Contributor

@jacob-carlborg jacob-carlborg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing Ddoc documentation for all new public symbols.

src/core/elf.d Outdated Show resolved Hide resolved
src/core/elf.d Outdated Show resolved Hide resolved
src/core/elf.d Outdated Show resolved Hide resolved
src/core/elf.d Outdated
}
}

dl_phdr_info info;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want this to be directly exposed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think so; rt.sections_elf_backtrace currently needs it for the TLS stuff, and I don't want to abstract it fully away, particularly since the struct is OS-dependent.

@dnadlinger
Copy link
Member

Do we really want this in core (i.e. user-exposed)? Even after the refactoring, the code consists mostly of cobbled-together pieces that fit our need rather than a universal API.

@kinke
Copy link
Contributor Author

kinke commented Oct 15, 2018

That was my main goal, letting the user have access to this functionality too. E.g., we could presumably use it in LDC to parse the dependencies of an .so to be linked; if it depends on shared druntime/Phobos, LDC would use -link-defaultlib-shared to link the binary against the shared default libs too, preventing the runtime error 'recompile with -link-defaultlib-shared' for executables compiled without it and linked with shared D libs.

@dnadlinger
Copy link
Member

That was my main goal, letting the user have access to this functionality too.

But why druntime over a separate library? Adding stuff into core.* tends to accrete quite a bit of scrutiny and discussion, and rightly so.

@kinke
Copy link
Contributor Author

kinke commented Oct 15, 2018

Well, because we need the stuff in druntime too. A higher level lib can always build on it.

@iain-buclaw-sociomantic

I don't think it's wise to have a platform-specific module in a common place.

@kinke
Copy link
Contributor Author

kinke commented Oct 16, 2018

I'm totally flexible wrt. final place of this; it could live in core.sys.elf or so too, it's just that I haven't found any suited existing dir yet (afaik not suited for Posix due to Apple's MachO).

@joakim-noah
Copy link
Contributor

Needs rebase and fix merge.

src/core/elf.d Outdated
@nogc nothrow:
this(ref const ElfFile file, size_t index)
{
assert(Elf_Shdr.sizeof == file.ehdr.e_shentsize);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tried my upcoming Android commit for #2325 with this pull, but it segfaults here: will look into why. In the meantime, turn this into an abort message, just as you did with #2324, so it exits immediately rather than repeatedly trying to throw an exception and then segfaulting. I'm doing the same in my Android pull, replacing asserts with abort messages.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is probably because this module currently only supports native ELF files, i.e., corresponding with the host, which is quite a serious limitation.

I don't think aborting here is a good idea; #2324 was different, as the chances for the GC not being initialized yet/already shut down are very high for that code, so throwing in that code is to be avoided.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm building on Android itself, but it's likely that some of the ELF definitions are different there.

As for aborting here, I will be using this on Android before the GC is initialized, but I didn't check how you're using it.

Copy link
Contributor Author

@kinke kinke Dec 15, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't matter how I'm using it; if this code is in core, the user is supposed to be able to use it too, so I don't want to abort in case the ELF file doesn't match; it might as well just be an attempt of opening the file as e.g. 32-bit ELF file.
So I should actually change the assertion to a normal throw (oh well, not sure how well that plays with nothrow and the backtrace code). Edit: Ah, that's the ElfSectionHeader ctor; using that one should be done after checking ElfFile.isValid(), so the assertion is fine IMO.

@joakim-noah
Copy link
Contributor

@kinke, any chance we can get this into the final 1.13 release? I'd like to use it for Android too.

@kinke
Copy link
Contributor Author

kinke commented Dec 6, 2018

I don't think so; I'm not too keen on having this pretty big diff wrt. upstream in LDC.

@joakim-noah
Copy link
Contributor

No, I mean do you think you can get it merged here in time for a cherrypick before the 1.13 release?

@kinke
Copy link
Contributor Author

kinke commented Dec 6, 2018

Lol, no way! :)

@joakim-noah
Copy link
Contributor

Heh, OK, I still need to look into why this pull doesn't work on Android anyway, maybe I'll just try to get it working for the native Android builds of LDC.

@dlang-bot dlang-bot removed the Needs Rebase needs a `git rebase` performed label Dec 8, 2018
@kinke kinke force-pushed the elf_upstream branch 2 times, most recently from 07c90db to e716ef7 Compare December 8, 2018 14:39
@dlang-bot dlang-bot added the Bug Fix Include reference to corresponding bugzilla issue label Dec 8, 2018
@kinke
Copy link
Contributor Author

kinke commented Jan 17, 2020

Rebased and moved to core.internal.elf.{dl,io}.

@PetarKirov
Copy link
Member

PetarKirov commented Jan 22, 2020

Let's stick to core.internal.elf.{dl,io} for now. We could always change the color of the bikeshed if/when we promote the module to a non-internal core package.

@kinke
Copy link
Contributor Author

kinke commented Feb 21, 2020

Ping ding dong, another month has passed by.

@dlang-bot dlang-bot merged commit f80a07e into dlang:master Feb 21, 2020
@kinke kinke deleted the elf_upstream branch February 21, 2020 15:07
@kinke kinke restored the elf_upstream branch March 12, 2020 19:44
@kinke kinke deleted the elf_upstream branch March 12, 2020 20:04
@CyberShadow
Copy link
Member

@kinke This pull request introduced a regression:
https://issues.dlang.org/show_bug.cgi?id=21656

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Bug Fix Include reference to corresponding bugzilla issue
Projects
None yet