aboutsummaryrefslogtreecommitdiff
path: root/llvm/tools/llvm-objcopy/ELF/Object.cpp
diff options
context:
space:
mode:
authorJordan Rupprecht <rupprecht@google.com>2019-06-10 18:35:01 +0000
committerJordan Rupprecht <rupprecht@google.com>2019-06-10 18:35:01 +0000
commitf8f9d65f85b66d843b4b4c6bb9aeb0ec1f19cbc3 (patch)
tree746244b39b3cb386fbb2d0f23d0d1894a390b4c0 /llvm/tools/llvm-objcopy/ELF/Object.cpp
parentb0f98d34225d8b61e6f82d0a683bb8d2b39cb408 (diff)
downloadllvm-f8f9d65f85b66d843b4b4c6bb9aeb0ec1f19cbc3.zip
llvm-f8f9d65f85b66d843b4b4c6bb9aeb0ec1f19cbc3.tar.gz
llvm-f8f9d65f85b66d843b4b4c6bb9aeb0ec1f19cbc3.tar.bz2
[llvm-objcopy] Fix SHT_GROUP ordering.
Summary: When llvm-objcopy sorts sections during finalization, it only sorts based on the offset, which can cause the group section to come after the sections it contains. This causes link failures when using gold to link objects created by llvm-objcopy. Fix this for now by copying GNU objcopy's behavior of placing SHT_GROUP sections first. In the future, we may want to remove this sorting entirely to more closely preserve the input file layout. This fixes https://bugs.llvm.org/show_bug.cgi?id=42052. Reviewers: jakehehrlich, jhenderson, MaskRay, espindola, alexshap Reviewed By: MaskRay Subscribers: phuongtrang148993, emaste, arichardson, llvm-commits Tags: #llvm Differential Revision: https://reviews.llvm.org/D62620 llvm-svn: 362973
Diffstat (limited to 'llvm/tools/llvm-objcopy/ELF/Object.cpp')
-rw-r--r--llvm/tools/llvm-objcopy/ELF/Object.cpp10
1 files changed, 8 insertions, 2 deletions
diff --git a/llvm/tools/llvm-objcopy/ELF/Object.cpp b/llvm/tools/llvm-objcopy/ELF/Object.cpp
index 89f25a4..05f2164 100644
--- a/llvm/tools/llvm-objcopy/ELF/Object.cpp
+++ b/llvm/tools/llvm-objcopy/ELF/Object.cpp
@@ -1668,9 +1668,15 @@ Error Object::removeSymbols(function_ref<bool(const Symbol &)> ToRemove) {
}
void Object::sortSections() {
- // Put all sections in offset order. Maintain the ordering as closely as
- // possible while meeting that demand however.
+ // Use stable_sort to maintain the original ordering as closely as possible.
llvm::stable_sort(Sections, [](const SecPtr &A, const SecPtr &B) {
+ // Put SHT_GROUP sections first, since group section headers must come
+ // before the sections they contain. This also matches what GNU objcopy
+ // does.
+ if (A->Type != B->Type &&
+ (A->Type == ELF::SHT_GROUP || B->Type == ELF::SHT_GROUP))
+ return A->Type == ELF::SHT_GROUP;
+ // For all other sections, sort by offset order.
return A->OriginalOffset < B->OriginalOffset;
});
}