Skip to content

test: add tests and docs for section address linker options#1145

Open
sai18022001 wants to merge 4 commits into
qualcomm:mainfrom
sai18022001:test/section-start-options
Open

test: add tests and docs for section address linker options#1145
sai18022001 wants to merge 4 commits into
qualcomm:mainfrom
sai18022001:test/section-start-options

Conversation

@sai18022001
Copy link
Copy Markdown

@sai18022001 sai18022001 commented May 7, 2026

Fixes #1099

Add missing SectionStart.test for Hexagon target
Add Common tests for -Ttext, -Tdata, -Tbss
Add Common test for -Ttext-segment
Add x86_64 tests for -Trodata-segment and -Tldata-segment
Update userguide layout.rst with descriptions for all options

Modifications to support this fix:
Add -Trodata-segment and -Tldata-segment option definitions and implementation
Implement -Ttext-segment option (required for tests to pass)

Note: When both -Ttext-segment and a linker script address assignment are
specified, the linker script takes precedence. This matches GNU ld behavior
and has been confirmed by the maintainer.

- Add tests for --section-start, -Ttext, -Tdata, -Tbss, -Ttext-segment
- Add -Trodata-segment and -Tldata-segment support and tests
- Update userguide with option descriptions

Resolves qualcomm#1099

Signed-off-by: Sai Sanjay Chikne <saichikne180201@gmail.com>

uint64_t GNULDBackend::getImageBase(bool HasInterp, bool LoadEHdr) const {
if (auto TextSegment = config().options().textSegment())
return *TextSegment;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Does -Ttext-segment override linker script assignment ?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I tested this, when both -Ttext-segment and a linker script address assignment are specified, the linker script takes precedence. Is this the expected behavior, or should -Ttext-segment always override the linker script ?

Copy link
Copy Markdown
Author

@sai18022001 sai18022001 May 9, 2026

Choose a reason for hiding this comment

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

Pinging on this - wanted to confirm the expected behavior before making any further changes to GNULDBackend.cpp.
Based on GNU ld behavior, the linker script takes precedence over -Ttext-segment when an explicit address is assigned in the script. The current implementation follows this behavior. Please let me know if a different behavior is expected for ELD.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks for confirming. Please go ahead.

Comment thread docs/userguide/documentation/layout.rst Outdated
Comment thread include/eld/Config/GeneralOptions.h Outdated

void setImageBase(uint64_t Value) { ImageBase = Value; }

const std::optional<uint64_t> &textSegment() const { return TextSegment; }
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Why is the PR for adding tests and docs modifying functionality?

Copy link
Copy Markdown
Author

@sai18022001 sai18022001 May 8, 2026

Choose a reason for hiding this comment

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

Good point.
Tests wouldn't pass without -Text-segment, -Trodata-segment and -Tldata-segment as it were not implemented previously
should i split them into 2 PR - one for tests/docs and one for implemetation ?
or you want to have a look at those implementations ?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think what @parth-07 is mentioning is the commit message says adding tests but now we are implementing support. The issue needs to be properly documented and also the commit message.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I'll update the commit message. Thanks for clarifying.

…options

- Add tests for --section-start, -Ttext, -Tdata, -Tbss, -Ttext-segment
- Add -Trodata-segment and -Tldata-segment support and tests
- Implement -Ttext-segment, -Trodata-segment, -Tldata-segment options
- Update userguide with option descriptions

Resolves qualcomm#1099

Signed-off-by: Sai Sanjay Chikne <saichikne180201@gmail.com>
@sai18022001
Copy link
Copy Markdown
Author

@quic-seaswara @parth-07
All review comments have been addressed. Requesting a re-review when you get a chance.

Signed-off-by: Sai Sanjay Chikne <saichikne180201@gmail.com>
@sai18022001 sai18022001 force-pushed the test/section-start-options branch from 40cf58c to ed54426 Compare May 13, 2026 09:45
Comment thread docs/userguide/documentation/layout.rst Outdated

--section-start=sectionname=org
Assigns the absolute address org to the named output section.
You may use this option multiple times to place multiple sections at specific addresses.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

What happens if you specify the --section-start with the same section name multiple times ? Can we add that to the documentation ?

You may use this option multiple times to place multiple sections at specific addresses.

-Ttext=org
Same as ``--section-start`` with ``.text`` as the section name.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

What if there is no .text section ?

Same as ``--section-start`` with ``.text`` as the section name.

-Tdata=org
Same as ``--section-start`` with ``.data`` as the section name.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Same as previous, what if there is no data section ?

Comment thread lib/LinkerWrapper/GnuLdDriver.cpp Outdated
if (llvm::opt::Arg *arg = Args.getLastArg(T::Trodata_segment)) {
uint64_t addr = 0;
if (!llvm::StringRef(arg->getValue()).getAsInteger(0, addr))
Config.options().setRodataSegment(addr);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Is this handled in program layout ?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

It is currently parsed and stored but not yet handled in program layout. Would you like me to implement it in this PR or open a separate issue for it?

Comment thread lib/LinkerWrapper/GnuLdDriver.cpp Outdated
if (llvm::opt::Arg *arg = Args.getLastArg(T::Tldata_segment)) {
uint64_t addr = 0;
if (!llvm::StringRef(arg->getValue()).getAsInteger(0, addr))
Config.options().setLdataSegment(addr);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Is this handled in program layout ?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

It is currently parsed and stored but not yet handled in program layout. Would you like me to implement it in this PR or open a separate issue for it?

Comment thread lib/LinkerWrapper/GnuLdDriver.cpp Outdated
if (llvm::opt::Arg *arg = Args.getLastArg(T::Ttext_segment)) {
uint64_t addr = 0;
if (!llvm::StringRef(arg->getValue()).getAsInteger(0, addr))
Config.options().setTextSegment(addr);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

It might be useful to give a warning with linker scripts if the user used a linker script and users using this option.

Comment thread lib/LinkerWrapper/GnuLdDriver.cpp Outdated
if (llvm::opt::Arg *arg = Args.getLastArg(T::Ttext_segment)) {
uint64_t addr = 0;
if (!llvm::StringRef(arg->getValue()).getAsInteger(0, addr))
Config.options().setTextSegment(addr);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Also add a diagnostic if the value is not represented as a integer or does not fit the target address space.

For example using a 64bit address on a 32bit image.

Comment thread include/eld/Driver/GnuLinkerOptions.td Outdated
Group<grp_scriptopts>;
defm Trodata_segment
: dashEqWithOpt<"Trodata-segment", "Trodata-segment", "Trodata_segment",
"Specify an address for the .rodata-segment segment">,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Can you reword this ? Is there anything like .rodata-segment ?

Comment thread include/eld/Driver/GnuLinkerOptions.td Outdated
Group<grp_scriptopts>;
defm Tldata_segment
: dashEqWithOpt<"Tldata-segment", "Tldata-segment", "Tldata_segment",
"Specify an address for the .ldata-segment segment">,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

What is .ldata-segment ? Can you simplify this and reword ?

Comment thread include/eld/Config/GeneralOptions.h Outdated

void setImageBase(uint64_t Value) { ImageBase = Value; }

const std::optional<uint64_t> &textSegment() const { return TextSegment; }
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Can we convert these functions to use verbs, get/set.

I recommend using getTextSegmentAddress(), it would be very clear.

Also update the text map file so that an user inspecting the text map file can know what is going on.

- Rename textSegment/rodataSegment/ldataSegment to getTextSegmentAddress/getRodataSegmentAddress/getLdataSegmentAddress
- Fix -Trodata-segment and -Tldata-segment option descriptions
- Update --section-start, -Ttext, -Tdata, -Tbss documentation

Signed-off-by: Sai Sanjay Chikne <saichikne180201@gmail.com>
@sai18022001 sai18022001 requested a review from quic-seaswara May 14, 2026 10:57
Copy link
Copy Markdown
Contributor

@parth-07 parth-07 left a comment

Choose a reason for hiding this comment

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

@sai18022001 Can you please move the tests to test/Common subdirectory?

Additionally, can you please update the PR description and the commit message to refect that this PR is adding the functionality instead of just adding test for the functionality.

// -Ttext-segment=value
if (llvm::opt::Arg *arg = Args.getLastArg(T::Ttext_segment)) {
uint64_t addr = 0;
if (!llvm::StringRef(arg->getValue()).getAsInteger(0, addr))
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

We should report an error if the argument is incorrect for the new options added.

}

uint64_t GNULDBackend::getImageBase(bool HasInterp, bool LoadEHdr) const {
if (auto TextSegment = config().options().getTextSegmentAddress())
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Can you please check what is the precedence between --image-base and -Ttext-segment in GNU ld?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Why is this test hexagon specific?

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.

Write tests to test and evaluate the following linker options

3 participants