Skip to content

feat: support --nmagic#1884

Open
TechnoPorg wants to merge 1 commit into
wild-linker:mainfrom
TechnoPorg:push-upkrlzsmuxnx
Open

feat: support --nmagic#1884
TechnoPorg wants to merge 1 commit into
wild-linker:mainfrom
TechnoPorg:push-upkrlzsmuxnx

Conversation

@TechnoPorg
Copy link
Copy Markdown
Contributor

--nmagic disables page alignment for loadable sections and prevents linking against shared libraries. Support for --omagic will follow once this is cleaned up and merged.

ld will merge the R and RX loadable segments with --nmagic, whereas lld will not. I have chosen to match the LLD behaviour.

I'm aware of two shortcomings so far in this PR:

  • Cross testing is disabled because QEMU apparently requires loadable segments to be page-aligned. This is rather unfortunate, as the tests in nmagic.c are passing on my x64 laptop, but failing in aarch64 CI, and I don't have an aarch64 box to test on.
  • ld and lld both don't write the PHDR program header entry with --nmagic, whereas wild with this PR does. Advice on how to match that behaviour would be appreciated.

Copy link
Copy Markdown
Member

@davidlattimore davidlattimore left a comment

Choose a reason for hiding this comment

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

Did you come across some software that uses this flag? It's the sort of flag that if someone is using it in software that's actively maintained, they should probably be working to migrate off of it. In that regard, it wouldn't say this is an essential flag for us to implement. That said, I don't object to implementing it, so long as it doesn't add substantial complexity.

Comment thread libwild/src/elf.rs
output_section_id::PROGRAM_HEADERS,
output_section_id::SECTION_HEADERS,
output_section_id::SHSTRTAB,
output_section_id::RELRO_PADDING,
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Why is RELRO_PADDING removed?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The short answer is that the x64 binaries segfault if it's not removed. The long answer is that a few lines down, the segment gets conditionally kept if args.relro is set, but since it's force kept above, the relro check doesn't have any effect. Is this something that should be split into another PR?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Ah, good catch. Yes, I think a separate PR for that would be good, that way we can make sure it's tested and list the change in the release notes.

Comment thread libwild/src/input_data.rs
Ok(InputRecord::Object(object))
if object.is_dynamic_object() && !input_ref.file.modifiers.allow_shared {
bail!(
"attempted static link of dynamic object {}",
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This looks like it's adding some error detection that we were perhaps missing before. It could be good to do this as a separate PR, ideally with a test to detect this error scenario.

Comment thread libwild/src/args/elf.rs
if args.nmagic {
// PT_GNU_RELRO requires segments to start on page boundaries and cover an entire page
args.relro = false;
if args.max_page_size.is_some() {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Checking this here means that we'll only error if max-page-size is set before nmagic. Probably best to check for invalid flag combinations after parsing is complete.

// GNU ld merges segment.LOAD.R and segment.LOAD.RX, which wild and lld do not
//#DiffIgnore: segment.LOAD.RX.alignment
//#DiffIgnore:segment.LOAD.RWX.alignment
// QEMU requires segments to be page-aligned
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Given that the binaries are segfaulting when run in CI, it seems likely that the cause is the same reason QEMU was rejecting them - i.e. lack of page alignment. Perhaps see if you can fix the page alignment, then maybe both qemu and CI will be happy.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Hmm. Given that --nmagic is there to disable page alignment, I'm not sure how best to test this...

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

If unaligned segments are a characteristic of the flag, but the hardware (and emulators) that we run on don't support running with unaligned segments, then perhaps we need "RunEnabled:false", then add whatever assertions we can about the output file to convince ourselves that it's correct.

@TechnoPorg
Copy link
Copy Markdown
Contributor Author

TechnoPorg commented May 4, 2026

Did you come across some software that uses this flag? It's the sort of flag that if someone is using it in software that's actively maintained, they should probably be working to migrate off of it. In that regard, it wouldn't say this is an essential flag for us to implement. That said, I don't object to implementing it, so long as it doesn't add substantial complexity.

It's part of a lot of embedded projects for boards with unaligned flash memory (source). While this is far from the largest blocker to using wild in embedded projects, it's one I felt was realistically within my capabilities to contribute.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants