Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion llvm/lib/ObjCopy/ELF/ELFObjcopy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -824,7 +824,7 @@ static Error handleArgs(const CommonConfig &Config, const ELFConfig &ELFConfig,

if (Config.ChangeSectionLMAValAll != 0) {
for (Segment &Seg : Obj.segments()) {
if (Seg.FileSize > 0) {
if (Seg.FileSize > 0 || Seg.MemSize > 0) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Should this check simply be if (Seg.MemSize > 0)? The difference would be that sections with file size and zero mem size would not be changed, but such segments are only valid if they are not PT_LOAD segments and presumably aren't part of the loaded file image.

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 didn't have a particularly principled reason for not doing this in the first place. Broadly, I was seeing such segments (non-PT_LOAD, filesize >0, memsize = 0) generated by ld.bfd for ex: the RISC-V PT_RISCV_ATTRIBUTES segment, and GNU objcopy seemed to modify the LMA but also would at times touch the offset/filesize/memsize in ways I didn't entirely follow. As such, I tried to err towards being consistent with our previous behavior here (to modify such segments).

That said, I think what you said about "presumably aren't part of the loaded file image" seems correct to me, so I went ahead and made the change/added an explicit test for this/updated the description--hopefully this looks reasonable!

if (Config.ChangeSectionLMAValAll > 0 &&
Seg.PAddr > std::numeric_limits<uint64_t>::max() -
Config.ChangeSectionLMAValAll) {
Expand Down
17 changes: 13 additions & 4 deletions llvm/test/tools/llvm-objcopy/ELF/change-section-lma.test
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,18 @@

# CHECK-PLUS-PROGRAMS: Type Offset VirtAddr PhysAddr FileSiz MemSiz
# CHECK-PLUS-PROGRAMS: PHDR 0x000002 0x0000000000001102 0x0000000000001122 0x000038 0x000000
# CHECK-PLUS-PROGRAMS: LOAD 0x000000 0x0000000000001100 0x0000000000001120 0x000258 0x000258
# CHECK-PLUS-PROGRAMS: LOAD 0x000258 0xffffffff00005100 0xffffffff00006120 0x000100 0x000100
# CHECK-PLUS-PROGRAMS: LOAD 0x000000 0x0000000000001100 0x0000000000001120 0x000290 0x000290
# CHECK-PLUS-PROGRAMS: LOAD 0x000290 0xffffffff00005100 0xffffffff00006120 0x000100 0x000100
# CHECK-PLUS-PROGRAMS: NOTE 0x000358 0x0000000000001200 0x0000000000001220 0x000010 0x000000
# CHECK-PLUS-PROGRAMS: NOTE 0x000368 0x0000000000000000 0x0000000000000000 0x000000 0x000000
# CHECK-PLUS-PROGRAMS: LOAD 0x000390 0x0000000000001300 0x0000000000001320 0x000000 0x000010

# CHECK-MINUS-PROGRAMS: PHDR 0x000002 0x0000000000001102 0x00000000000010d2 0x000038 0x000000
# CHECK-MINUS-PROGRAMS: LOAD 0x000000 0x0000000000001100 0x00000000000010d0 0x000258 0x000258
# CHECK-MINUS-PROGRAMS: LOAD 0x000258 0xffffffff00005100 0xffffffff000060d0 0x000100 0x000100
# CHECK-MINUS-PROGRAMS: LOAD 0x000000 0x0000000000001100 0x00000000000010d0 0x000290 0x000290
# CHECK-MINUS-PROGRAMS: LOAD 0x000290 0xffffffff00005100 0xffffffff000060d0 0x000100 0x000100
# CHECK-MINUS-PROGRAMS: NOTE 0x000358 0x0000000000001200 0x00000000000011d0 0x000010 0x000000
# CHECK-MINUS-PROGRAMS: NOTE 0x000368 0x0000000000000000 0x0000000000000000 0x000000 0x000000
# CHECK-MINUS-PROGRAMS: LOAD 0x000390 0x0000000000001300 0x00000000000012d0 0x000000 0x000010

# CHECK-PLUS-SECTIONS: [Nr] Name Type Address Off Size ES Flg Lk Inf Al
# CHECK-PLUS-SECTIONS: .text1
Expand Down Expand Up @@ -55,6 +57,9 @@ Sections:
- Name: .text2
Type: SHT_PROGBITS
Size: 0x100
- Name: .bss1
Type: SHT_NOBITS
Size: 0x10
ProgramHeaders:
- Type: PT_PHDR
FileSize: 0x38
Expand All @@ -77,3 +82,7 @@ ProgramHeaders:
- Type: PT_NOTE
FileSize: 0x0
Offset: 0x368
- Type: PT_LOAD
VAddr: 0x1300
FirstSec: .bss1
LastSec: .bss1