Skip to content

Skip 32 bit relocation addend partitioning in MCCAS. #8444

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
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
54 changes: 35 additions & 19 deletions llvm/lib/MCCAS/MCCASObjectV1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2433,6 +2433,15 @@ Error MCCASBuilder::buildFragments() {
continue;

SmallVector<char, 0> FinalFragmentContents;
// Set the RelocationBuffer to be an empty ArrayRef, and the
// RelocationBufferIndex to zero if the architecture is 32-bit, because we
// do not support relocation partitioning on 32-bit platforms. With this,
// partitionFragment will put all the fragment contents in the
// FinalFragmentContents, and the Addends buffer will be empty.
if (ObjectWriter.getAddressSize() == 4) {
RelocationBuffer = ArrayRef<MachO::any_relocation_info>();
RelocationBufferIndex = 0;
}
partitionFragment(Layout, Addends, FinalFragmentContents,
RelocationBuffer, F, RelocationBufferIndex,
ObjectWriter.Target.isLittleEndian());
Expand Down Expand Up @@ -2834,25 +2843,32 @@ MCCASReader::reconstructSection(SmallVectorImpl<char> &SectionBuffer,
/// copied the addend out of the Addends at a particular offset, we should
/// skip all relocations that matches the same offset.
int64_t PrevOffset = -1;
for (auto Reloc : Relocations.back()) {
auto RelocationOffsetInSection = getRelocationOffset(Reloc);
if (PrevOffset == RelocationOffsetInSection)
continue;
auto RelocationSize =
getRelocationSize(Reloc, getEndian() == support::little);
/// NumOfBytesToReloc: This denotes the number of bytes needed to be copied
/// into the \p SectionBuffer before we copy the next addend.
auto NumOfBytesToReloc = RelocationOffsetInSection - SectionBuffer.size();
// Copy the contents of the fragment till the next relocation.
SectionBuffer.append(FragmentBuffer.begin() + FragmentIndex,
FragmentBuffer.begin() + FragmentIndex +
NumOfBytesToReloc);
FragmentIndex += NumOfBytesToReloc;
// Copy the relocation addend.
SectionBuffer.append(Addends.begin() + AddendBufferIndex,
Addends.begin() + AddendBufferIndex + RelocationSize);
AddendBufferIndex += RelocationSize;
PrevOffset = RelocationOffsetInSection;
/// If the \p Addends buffer is empty, there was no AddendsRef for this
/// section, this is either because no \p Relocations exist in this section,
/// or this is 32-bit architecture, where we do not support relocation
/// partitioning.
if (!Addends.empty()) {
for (auto Reloc : Relocations.back()) {
auto RelocationOffsetInSection = getRelocationOffset(Reloc);
if (PrevOffset == RelocationOffsetInSection)
continue;
auto RelocationSize =
getRelocationSize(Reloc, getEndian() == support::little);
/// NumOfBytesToReloc: This denotes the number of bytes needed to be
/// copied into the \p SectionBuffer before we copy the next addend.
auto NumOfBytesToReloc = RelocationOffsetInSection - SectionBuffer.size();
// Copy the contents of the fragment till the next relocation.
SectionBuffer.append(FragmentBuffer.begin() + FragmentIndex,
FragmentBuffer.begin() + FragmentIndex +
NumOfBytesToReloc);
FragmentIndex += NumOfBytesToReloc;
// Copy the relocation addend.
SectionBuffer.append(Addends.begin() + AddendBufferIndex,
Addends.begin() + AddendBufferIndex +
RelocationSize);
AddendBufferIndex += RelocationSize;
PrevOffset = RelocationOffsetInSection;
}
}
// Copy any remaining bytes of the fragment into the SectionBuffer.
SectionBuffer.append(FragmentBuffer.begin() + FragmentIndex,
Expand Down
9 changes: 9 additions & 0 deletions llvm/test/CAS/test-reloc-mccas-32bit-arm.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# This test tests to make sure mccas can handle scattered relocations properly on 32 bit arm

# RUN: rm -rf %t && mkdir -p %t
# RUN: llvm-mc --cas=%t/cas --cas-backend --mccas-verify -triple=armv7-apple-darwin10 -filetype=obj -o %t/reloc.o %s

movw r0, :lower16:(fn2-L1)
L1:
fn2:

6 changes: 6 additions & 0 deletions llvm/test/CAS/test-reloc-mccas-32bit.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# This test tests to make sure mccas can handle scattered relocations properly on 32 bit x86

# RUN: rm -rf %t && mkdir -p %t
# RUN: llvm-mc --cas=%t/cas --cas-backend --mccas-verify -triple=i386-apple-macosx10.4 -filetype=obj -o %t/reloc.o %s
movl y+4, %ecx
.zerofill __DATA,__common,y,8,3