This is an archive of the discontinued LLVM Phabricator instance.

[ELF] Allow redefining LMA for a section inside a segment.
Needs ReviewPublic

Authored by ikudrin on Feb 13 2018, 2:47 AM.

Details

Summary

We have a constraint that all sections within a segment have the same difference between LMA and VMA.
If we encounter a section with a customized LMA, we might create a new segment, but not in the case
when all program headers are explicitly defined by a linker script. In that case, the setting is just ignored.

This patch allows sections within one segment to have different LMA offsets. The behavior is similar to GNU ld's.

The patch, technically, reverts r323456 and fixes PR36017 in a different. way.

Diff Detail

Event Timeline

ikudrin created this revision.Feb 13 2018, 2:47 AM
ikudrin updated this revision to Diff 134380.Feb 14 2018, 10:59 PM
ikudrin retitled this revision from [ELF] Allow redefining LMA for a section within a segment. to [ELF] Allow redefining LMA for a section inside a segment..
  • Adjust to the tip.
ruiu added a comment.Feb 15 2018, 9:01 PM

The ELF spec [1] requires that "loadable process segments must have congruent values for p_vaddr and p_offset, modulo the page size." Doesn't the executable produced with this patch violate that rule?

[1] http://www.skyfree.org/linux/references/ELF_Format.pdf p. 2

It's not for an application. We use it for a kernel image.

ruiu added a comment.Feb 15 2018, 9:12 PM

Do you need to create an ELF file that doesn't fully comply with the spec?

I don't see anything that violates the specs. With this patch, the offset of a segment (p_offset) is still congruent to its virtual address (p_vaddr), modulo page size. This rule is enforced in lines 1850-1852 of ELF/Writer.cpp:

if (Cmd == First)
  return alignTo(Off, std::max<uint64_t>(Cmd->Alignment, Config->MaxPageSize),
                 Cmd->Addr);

and I didn't touch them.

The description for AT output section attribute says that it defines a load address of the section and that this feature is designed to simplify creating a ROM image. This is exactly what we use it for. https://sourceware.org/binutils/docs/ld/Output-Section-LMA.html

If there are no prescribed program headers in a linker script, the linker creates a new segment on each section with customized LMA, and this is OK and expected (D43284). But in case of explicitly defined segments, we, in fact, ignore some of AT directives. And this contradicts the user's intention.

espindola edited reviewers, added: espindola; removed: rafael.Mar 15 2018, 8:36 AM