Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[lldb] Set static Module's load addresses via ObjectFile #87439

Conversation

jasonmolenda
Copy link
Collaborator

This is a followup to
#86359
"[lldb] [ObjectFileMachO] LLVM_COV is not mapped into firmware memory (#86359)"

where I treat LLVM_COV segments in a Mach-O binary as non-loadable. There is another codepath in
DynamicLoaderStatic::LoadAllImagesAtFileAddresses which is called to set the load addresses for a Module to the file addresses. It has no logic to detect a segment that is not loaded in virtual memory (ObjectFileMachO::SectionIsLoadable), so it would set the load address for this LLVM_COV segment to the file address and shadow actual code, breaking lldb behavior.

This method currently sets the load address for any section that doesn't have a load address set already. This presumes that a Module was added to the Target, some mechanism set the correct load address for SOME segments, and then this method is going to set the other segments to a no-slide value, assuming they were forgotten.

ObjectFile base class doesn't, today, vend a SectionIsLoadable method, but we do have ObjectFile::SetLoadAddress and at a higher level, Module::SetLoadAddress, when we're setting the same slide to all segments.

That's the behavior we want in this method. If any section has a load address, we don't touch this Module. Otherwise we set all sections to have a load address that is the same as the file address.

I also audited the other parts of lldb that are calling SectionList::SectionLoadAddress and looked if they should be more correctly using Module::SetLoadAddress for the entire binary. But in most cases, we have the potential for different slides for different sections so this section-by-section approach must be taken.

rdar://125800290

This is a followup to
llvm#86359
"[lldb] [ObjectFileMachO] LLVM_COV is not mapped into firmware memory
(llvm#86359)"

where I treat LLVM_COV segments in a Mach-O binary as non-loadable.
There is another codepath in
`DynamicLoaderStatic::LoadAllImagesAtFileAddresses` which is called
to set the load addresses for a Module to the file addresses.  It
has no logic to detect a segment that is not loaded in virtual
memory (ObjectFileMachO::SectionIsLoadable), so it would set the
load address for this LLVM_COV segment to the file address and
shadow actual code, breaking lldb behavior.

This method currently sets the load address for any section that
doesn't have a load address set already.  This presumes that a
Module was added to the Target, some mechanism set the correct load
address for SOME segments, and then this method is going to set the
other segments to a no-slide value, assuming they were forgotten.

ObjectFile base class doesn't, today, vend a SectionIsLoadable
method, but we do have ObjectFile::SetLoadAddress and at a higher
level, Module::SetLoadAddress, when we're setting the same slide
to all segments.

That's the behavior we want in this method.  If any section has a
load address, we don't touch this Module.  Otherwise we set all
sections to have a load address that is the same as the file address.

I also audited the other parts of lldb that are calling
SectionList::SectionLoadAddress and looked if they should be more
correctly using Module::SetLoadAddress for the entire binary.  But in
most cases, we have the potential for different slides for different
sections so this section-by-section approach must be taken.

rdar://125800290
Copy link
Member

@JDevlieghere JDevlieghere left a comment

Choose a reason for hiding this comment

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

LGTM

@jasonmolenda jasonmolenda merged commit 622851a into llvm:main Apr 3, 2024
5 checks passed
@jasonmolenda jasonmolenda deleted the simplify-dynamicloaderstatic-no-slide-loadaddr-setter branch April 3, 2024 23:40
jasonmolenda added a commit to jasonmolenda/llvm-project that referenced this pull request Apr 4, 2024
This is a followup to
llvm#86359
"[lldb] [ObjectFileMachO] LLVM_COV is not mapped into firmware memory
(llvm#86359)"

where I treat LLVM_COV segments in a Mach-O binary as non-loadable.
There is another codepath in
`DynamicLoaderStatic::LoadAllImagesAtFileAddresses` which is called to
set the load addresses for a Module to the file addresses. It has no
logic to detect a segment that is not loaded in virtual memory
(ObjectFileMachO::SectionIsLoadable), so it would set the load address
for this LLVM_COV segment to the file address and shadow actual code,
breaking lldb behavior.

This method currently sets the load address for any section that doesn't
have a load address set already. This presumes that a Module was added
to the Target, some mechanism set the correct load address for SOME
segments, and then this method is going to set the other segments to a
no-slide value, assuming they were forgotten.

ObjectFile base class doesn't, today, vend a SectionIsLoadable method,
but we do have ObjectFile::SetLoadAddress and at a higher level,
Module::SetLoadAddress, when we're setting the same slide to all
segments.

That's the behavior we want in this method. If any section has a load
address, we don't touch this Module. Otherwise we set all sections to
have a load address that is the same as the file address.

I also audited the other parts of lldb that are calling
SectionList::SectionLoadAddress and looked if they should be more
correctly using Module::SetLoadAddress for the entire binary. But in
most cases, we have the potential for different slides for different
sections so this section-by-section approach must be taken.

rdar://125800290
(cherry picked from commit 622851a)
jasonmolenda added a commit to jasonmolenda/llvm-project that referenced this pull request Apr 4, 2024
This is a followup to
llvm#86359
"[lldb] [ObjectFileMachO] LLVM_COV is not mapped into firmware memory
(llvm#86359)"

where I treat LLVM_COV segments in a Mach-O binary as non-loadable.
There is another codepath in
`DynamicLoaderStatic::LoadAllImagesAtFileAddresses` which is called to
set the load addresses for a Module to the file addresses. It has no
logic to detect a segment that is not loaded in virtual memory
(ObjectFileMachO::SectionIsLoadable), so it would set the load address
for this LLVM_COV segment to the file address and shadow actual code,
breaking lldb behavior.

This method currently sets the load address for any section that doesn't
have a load address set already. This presumes that a Module was added
to the Target, some mechanism set the correct load address for SOME
segments, and then this method is going to set the other segments to a
no-slide value, assuming they were forgotten.

ObjectFile base class doesn't, today, vend a SectionIsLoadable method,
but we do have ObjectFile::SetLoadAddress and at a higher level,
Module::SetLoadAddress, when we're setting the same slide to all
segments.

That's the behavior we want in this method. If any section has a load
address, we don't touch this Module. Otherwise we set all sections to
have a load address that is the same as the file address.

I also audited the other parts of lldb that are calling
SectionList::SectionLoadAddress and looked if they should be more
correctly using Module::SetLoadAddress for the entire binary. But in
most cases, we have the potential for different slides for different
sections so this section-by-section approach must be taken.

rdar://125800290
(cherry picked from commit 622851a)
jasonmolenda added a commit to swiftlang/llvm-project that referenced this pull request Apr 4, 2024
…ress-setter-in-dynamicloaderstatic-0725

[lldb] Set static Module's load addresses via ObjectFile (llvm#87439)
JDevlieghere added a commit to swiftlang/llvm-project that referenced this pull request Apr 8, 2024
…ress-setter-in-dynamicloaderstatic-6.0

[lldb] Set static Module's load addresses via ObjectFile (llvm#87439)
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