Skip to content

Commit 366df11

Browse files
committed
[lld-macho] Rework mergeFlag to behave closer to what ld64 does.
Details: I've been getting a few weird errors similar to the following from our internal tests: ``` ld64.lld.darwinnew: error: Cannot merge section __eh_frame (type=0x0) into __eh_frame (type=0xB): inconsistent types ld64.lld.darwinnew: error: Cannot merge section __eh_frame (flags=0x0) into __eh_frame (flags=0x6800000B): strict flags differ ld64.lld.darwinnew: error: Cannot merge section __eh_frame (type=0x0) into __eh_frame (type=0xB): inconsistent types ld64.lld.darwinnew: error: Cannot merge section __eh_frame (flags=0x0) into __eh_frame (flags=0x6800000B): strict flags differ ``` Differential Revision: https://reviews.llvm.org/D103971
1 parent d02bf36 commit 366df11

File tree

3 files changed

+25
-46
lines changed

3 files changed

+25
-46
lines changed

lld/MachO/ConcatOutputSection.cpp

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ void ConcatOutputSection::addInput(ConcatInputSection *input) {
3030
flags = input->flags;
3131
} else {
3232
align = std::max(align, input->align);
33-
mergeFlags(input);
33+
finalizeFlags(input);
3434
}
3535
inputs.push_back(input);
3636
input->parent = this;
@@ -331,30 +331,29 @@ void ConcatOutputSection::writeTo(uint8_t *buf) const {
331331
}
332332
}
333333

334-
// TODO: this is most likely wrong; reconsider how section flags
335-
// are actually merged. The logic presented here was written without
336-
// any form of informed research.
337-
void ConcatOutputSection::mergeFlags(InputSection *input) {
338-
uint8_t baseType = sectionType(flags);
339-
uint8_t inputType = sectionType(input->flags);
340-
if (baseType != inputType)
341-
error("Cannot merge section " + input->name + " (type=0x" +
342-
to_hexString(inputType) + ") into " + name + " (type=0x" +
343-
to_hexString(baseType) + "): inconsistent types");
344-
345-
constexpr uint32_t strictFlags = S_ATTR_DEBUG | S_ATTR_STRIP_STATIC_SYMS |
346-
S_ATTR_NO_DEAD_STRIP | S_ATTR_LIVE_SUPPORT;
347-
if ((input->flags ^ flags) & strictFlags)
348-
error("Cannot merge section " + input->name + " (flags=0x" +
349-
to_hexString(input->flags) + ") into " + name + " (flags=0x" +
350-
to_hexString(flags) + "): strict flags differ");
351-
352-
// Negate pure instruction presence if any section isn't pure.
353-
uint32_t pureMask = ~S_ATTR_PURE_INSTRUCTIONS | (input->flags & flags);
354-
355-
// Merge the rest
356-
flags |= input->flags;
357-
flags &= pureMask;
334+
void ConcatOutputSection::finalizeFlags(InputSection *input) {
335+
uint8_t inputType = input->flags & SECTION_TYPE;
336+
switch (inputType) {
337+
default /*type-unspec'ed*/:
338+
// FIXME: Add additional logics here when supporting emitting obj files.
339+
break;
340+
case S_4BYTE_LITERALS:
341+
case S_8BYTE_LITERALS:
342+
case S_16BYTE_LITERALS:
343+
case S_CSTRING_LITERALS:
344+
case S_ZEROFILL:
345+
case S_LAZY_SYMBOL_POINTERS:
346+
case S_MOD_TERM_FUNC_POINTERS:
347+
case S_THREAD_LOCAL_REGULAR:
348+
case S_THREAD_LOCAL_ZEROFILL:
349+
case S_THREAD_LOCAL_VARIABLES:
350+
case S_THREAD_LOCAL_INIT_FUNCTION_POINTERS:
351+
case S_THREAD_LOCAL_VARIABLE_POINTERS:
352+
case S_NON_LAZY_SYMBOL_POINTERS:
353+
case S_SYMBOL_STUBS:
354+
flags |= input->flags;
355+
break;
356+
}
358357
}
359358

360359
void ConcatOutputSection::eraseOmittedInputSections() {

lld/MachO/ConcatOutputSection.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ class ConcatOutputSection final : public OutputSection {
5252
}
5353

5454
private:
55-
void mergeFlags(InputSection *input);
55+
void finalizeFlags(InputSection *input);
5656

5757
size_t size = 0;
5858
uint64_t fileSize = 0;

lld/test/MachO/builtin-rename.s

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,6 @@
55
# RUN: %t/main.s -o %t/main.o
66
# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin \
77
# RUN: %t/renames.s -o %t/renames.o
8-
# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin \
9-
# RUN: %t/error.s -o %t/error.o
10-
11-
# RUN: not %lld -o %t/error %t/main.o %t/error.o -lSystem 2>&1 | \
12-
# RUN: FileCheck %s --check-prefix=ERROR
13-
14-
## Check the error diagnostic for merging mismatched section types
15-
# ERROR: Cannot merge section __pointers (type=0x0) into __nl_symbol_ptr (type=0x6): inconsistent types
168

179
## Check that section and segment renames happen as expected
1810
# RUN: %lld -o %t/ydata %t/main.o %t/renames.o -lSystem
@@ -151,18 +143,6 @@ __IMPORT__pointers:
151143
__TEXT__StaticInit:
152144
.space 8
153145

154-
#--- error.s
155-
156-
.section __DATA,__nl_symbol_ptr
157-
.global __DATA__nl_symbol_ptr
158-
__DATA__nl_symbol_ptr:
159-
.space 8
160-
161-
.section __IMPORT,__pointers
162-
.global __IMPORT__pointers
163-
__IMPORT__pointers:
164-
.space 8
165-
166146
#--- main.s
167147
.text
168148
.global _main

0 commit comments

Comments
 (0)