Skip to content

Commit

Permalink
Revert "[llvm-objcopy] -O binary: do not align physical addresses"
Browse files Browse the repository at this point in the history
This reverts commit eb1442d.

The test tools/llvm-objcopy/ELF/binary-paddr.test fails on
ppc64be-clang-test-suite:
https://lab.llvm.org/buildbot#builders/231/builds/13120

Reverting at author's request.
  • Loading branch information
Krzysztof Parzyszek committed Jun 20, 2023
1 parent e28bb6c commit ebc757d
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 64 deletions.
12 changes: 2 additions & 10 deletions llvm/lib/ObjCopy/ELF/ELFObjcopy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,6 @@ static uint64_t getSectionFlagsPreserveMask(uint64_t OldFlags,
return (OldFlags & PreserveMask) | (NewFlags & ~PreserveMask);
}

static void setSectionType(SectionBase &Sec, uint64_t Type) {
// If Sec's type is changed from SHT_NOBITS due to --set-section-flags,
// Offset may not be aligned. Align it to max(Align, 1).
if (Sec.Type == ELF::SHT_NOBITS && Type != ELF::SHT_NOBITS)
Sec.Offset = alignTo(Sec.Offset, std::max(Sec.Align, uint64_t(1)));
Sec.Type = Type;
}

static void setSectionFlagsAndType(SectionBase &Sec, SectionFlag Flags) {
Sec.Flags = getSectionFlagsPreserveMask(Sec.Flags, getNewShfFlags(Flags));

Expand All @@ -114,7 +106,7 @@ static void setSectionFlagsAndType(SectionBase &Sec, SectionFlag Flags) {
if (Sec.Type == SHT_NOBITS &&
(!(Sec.Flags & ELF::SHF_ALLOC) ||
Flags & (SectionFlag::SecContents | SectionFlag::SecLoad)))
setSectionType(Sec, ELF::SHT_PROGBITS);
Sec.Type = SHT_PROGBITS;
}

static ElfType getOutputElfType(const Binary &Bin) {
Expand Down Expand Up @@ -692,7 +684,7 @@ static Error handleArgs(const CommonConfig &Config, const ELFConfig &ELFConfig,
}
auto It2 = Config.SetSectionType.find(Sec.Name);
if (It2 != Config.SetSectionType.end())
setSectionType(Sec, It2->second);
Sec.Type = It2->second;
}
}

Expand Down
7 changes: 5 additions & 2 deletions llvm/lib/ObjCopy/ELF/ELFObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2652,9 +2652,12 @@ Error BinaryWriter::finalize() {
// MinAddr will be skipped.
uint64_t MinAddr = UINT64_MAX;
for (SectionBase &Sec : Obj.allocSections()) {
// If Sec's type is changed from SHT_NOBITS due to --set-section-flags,
// Offset may not be aligned. Align it to max(Align, 1).
if (Sec.ParentSegment != nullptr)
Sec.Addr =
Sec.Offset - Sec.ParentSegment->Offset + Sec.ParentSegment->PAddr;
Sec.Addr = alignTo(Sec.Offset - Sec.ParentSegment->Offset +
Sec.ParentSegment->PAddr,
std::max(Sec.Align, uint64_t(1)));
if (Sec.Type != SHT_NOBITS && Sec.Size > 0)
MinAddr = std::min(MinAddr, Sec.Addr);
}
Expand Down
52 changes: 0 additions & 52 deletions llvm/test/tools/llvm-objcopy/ELF/binary-paddr.test
Original file line number Diff line number Diff line change
Expand Up @@ -254,55 +254,3 @@ ProgramHeaders:
FileSize: 0x2
MemSize: 0x13
Align: 0x1000

## Test that sections do not move relative to their physical addresses if
## the physical address is not aligned. This behavior matches GNU objcopy
## and is important for embedded images where the physical address is
## used to store the initial data image. Without converting the type of a
## NOBITS section, don't align the offset. This matches GNU objcopy when
## the physical address of a section is not aligned.

# RUN: yaml2obj --docnum=8 %s -o %t8
# RUN: llvm-objcopy -O binary --only-section=.text --only-section=.data %t8 %t8.out
# RUN: od -A x -t x2 %t8.out | FileCheck %s --check-prefix=PHYSUNALIGNED --ignore-case

# PHYSUNALIGNED: 000000 2211 4433 6655 8877 aa99 ccbb eedd 00ff
# PHYSUNALIGNED-NEXT: 000010 2211 4433 acbd abcd

--- !ELF
FileHeader:
Class: ELFCLASS64
Data: ELFDATA2LSB
Type: ET_EXEC
Machine: EM_X86_64
ProgramHeaders:
- Type: PT_LOAD
Flags: [ PF_X, PF_R ]
FirstSec: .text
LastSec: .text
VAddr: 0x200000
PAddr: 0x200000
Align: 0x1000
- Type: PT_LOAD
Flags: [ PF_W, PF_R ]
FirstSec: .data
LastSec: .data
VAddr: 0x400000
PAddr: 0x200014
## PAddr is not aligned by sh_addralign(.data)
Align: 0x1000
Sections:
- Name: .text
Type: SHT_PROGBITS
Flags: [ SHF_ALLOC, SHF_EXECINSTR ]
Address: 0x200000
AddressAlign: 0x1
Offset: 0x1000
Content: 112233445566778899aabbccddeeff0011223344
- Name: .data
Type: SHT_PROGBITS
Flags: [ SHF_WRITE, SHF_ALLOC ]
Address: 0x400000
AddressAlign: 0x8
Offset: 0x2000
Content: BDACCDAB

0 comments on commit ebc757d

Please sign in to comment.