diff --git a/lld/MachO/SyntheticSections.cpp b/lld/MachO/SyntheticSections.cpp --- a/lld/MachO/SyntheticSections.cpp +++ b/lld/MachO/SyntheticSections.cpp @@ -280,6 +280,7 @@ struct BindOpcodeOpt { uint8_t opcode = 0xF0; uint64_t delta = 0; // Placeholder for offset or addend + uint64_t consecutiveCount = 0; }; } // namespace @@ -330,11 +331,56 @@ lastBinding.offset += target->wordSize; } +static void optimizeOpcodes(std::vector &opcodes) { + // Pass 1: Combine bind/add pairs + size_t i; + int pWrite = 0; + for (i = 1; i < opcodes.size(); ++i, ++pWrite) { + if ((opcodes[i].opcode == BIND_OPCODE_ADD_ADDR_ULEB) + && (opcodes[i-1].opcode == BIND_OPCODE_DO_BIND)) { + opcodes[pWrite].opcode = BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB; + opcodes[pWrite].delta = opcodes[i].delta; + ++i; + } else { + opcodes[pWrite] = opcodes[i-1]; + } + } + if (i == opcodes.size()) { + opcodes[pWrite] = opcodes[i-1]; + } + opcodes.resize(pWrite+1); + + // Pass 2: Compress two or more bind_add opcodes + pWrite = 0; + for (i = 1; i < opcodes.size(); ++i, ++pWrite) { + if ((opcodes[i].opcode == BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB) + && (opcodes[i-1].opcode == BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB) + && (opcodes[i].delta == opcodes[i-1].delta)) { + opcodes[pWrite].opcode = BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB; + opcodes[pWrite].consecutiveCount = 2; + opcodes[pWrite].delta = opcodes[i].delta; + ++i; + while (i < opcodes.size() && (opcodes[i].opcode == BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB) + && (opcodes[i].delta == opcodes[i-1].delta)) { + opcodes[pWrite].consecutiveCount++; + ++i; + } + } else { + opcodes[pWrite] = opcodes[i-1]; + } + } + if (i == opcodes.size()) { + opcodes[pWrite] = opcodes[i-1]; + } + opcodes.resize(pWrite+1); +} + static void flushOpcodes(BindOpcodeOpt &op, raw_svector_ostream &os) { uint8_t opcode = op.opcode & 0xF0; switch (opcode) { case BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB: case BIND_OPCODE_ADD_ADDR_ULEB: + case BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB: os << op.opcode; encodeULEB128(op.delta, os); break; @@ -345,6 +391,11 @@ case BIND_OPCODE_DO_BIND: os << op.opcode; break; + case BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB: + os << op.opcode; + encodeULEB128(op.consecutiveCount, os); + encodeULEB128(op.delta, os); + break; default: llvm_unreachable("cannot bind to an unrecognized symbol"); } @@ -444,6 +495,7 @@ encodeBinding(b.target.isec->parent, b.target.isec->getOffset(b.target.offset), b.addend, lastBinding, opcodes); + optimizeOpcodes(opcodes); for (auto &op : opcodes) flushOpcodes(op, os); } @@ -476,6 +528,7 @@ encodeBinding(b.target.isec->parent, b.target.isec->getOffset(b.target.offset), b.addend, lastBinding, opcodes); + optimizeOpcodes(opcodes); for (auto &op : opcodes) flushOpcodes(op, os); } diff --git a/lld/test/MachO/bind-opcodes.s b/lld/test/MachO/bind-opcodes.s --- a/lld/test/MachO/bind-opcodes.s +++ b/lld/test/MachO/bind-opcodes.s @@ -5,25 +5,80 @@ # RUN: %lld -dylib %t/foo.o -o %t/libfoo.dylib # RUN: %lld -lSystem %t/test.o %t/libfoo.dylib -o %t/test -## Make sure we emit exactly one BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM per -## symbol. -# RUN: obj2yaml %t/test | FileCheck %s --implicit-check-not BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM +## Test: +## 1/ We emit exactly one BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM per symbol. +## 2/ Combine BIND_OPCODE_DO_BIND and BIND_OPCODE_ADD_ADDR_ULEB pairs. +## 3/ Compact BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB +# RUN: obj2yaml %t/test | FileCheck %s -# CHECK: Opcode: BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM -# CHECK-NEXT: Imm: 0 -# CHECK-NEXT: Symbol: _foo - -# CHECK: Opcode: BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM -# CHECK-NEXT: Imm: 0 -# CHECK-NEXT: Symbol: _bar +# CHECK: BindOpcodes: +# CHECK-NEXT: Opcode: BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM +# CHECK-NEXT: Imm: 0 +# CHECK-NEXT: Symbol: _foo +# CHECK-NEXT: Opcode: BIND_OPCODE_SET_TYPE_IMM +# CHECK-NEXT: Imm: 1 +# CHECK-NEXT: Symbol: '' +# CHECK-NEXT: Opcode: BIND_OPCODE_SET_DYLIB_ORDINAL_IMM +# CHECK-NEXT: Imm: 2 +# CHECK-NEXT: Symbol: '' +# CHECK-NEXT: Opcode: BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB +# CHECK-NEXT: Imm: 2 +# CHECK-NEXT: ULEBExtraData: [ 0x0 ] +# CHECK-NEXT: Symbol: '' +# CHECK-NEXT: Opcode: BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB +# CHECK-NEXT: Imm: 0 +# CHECK-NEXT: ULEBExtraData: [ 0x2, 0x8 ] +# CHECK-NEXT: Symbol: '' +# CHECK-NEXT: Opcode: BIND_OPCODE_SET_ADDEND_SLEB +# CHECK-NEXT: Imm: 0 +# CHECK-NEXT: SLEBExtraData: [ 1 ] +# CHECK-NEXT: Symbol: '' +# CHECK-NEXT: Opcode: BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB +# CHECK-NEXT: Imm: 0 +# CHECK-NEXT: ULEBExtraData: [ 0x1008 ] +# CHECK-NEXT: Symbol: '' +# CHECK-NEXT: Opcode: BIND_OPCODE_SET_ADDEND_SLEB +# CHECK-NEXT: Imm: 0 +# CHECK-NEXT: SLEBExtraData: [ 0 ] +# CHECK-NEXT: Symbol: '' +# CHECK-NEXT: Opcode: BIND_OPCODE_DO_BIND +# CHECK-NEXT: Imm: 0 +# CHECK-NEXT: Symbol: '' +# CHECK-NEXT: Opcode: BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM +# CHECK-NEXT: Imm: 0 +# CHECK-NEXT: Symbol: _bar +# CHECK-NEXT: Opcode: BIND_OPCODE_SET_TYPE_IMM +# CHECK-NEXT: Imm: 1 +# CHECK-NEXT: Symbol: '' +# CHECK-NEXT: Opcode: BIND_OPCODE_ADD_ADDR_ULEB +# CHECK-NEXT: Imm: 0 +# CHECK-NEXT: ULEBExtraData: [ 0xFFFFFFFFFFFFEFD0 ] +# CHECK-NEXT: Symbol: '' +# CHECK-NEXT: Opcode: BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB +# CHECK-NEXT: Imm: 0 +# CHECK-NEXT: ULEBExtraData: [ 0x8 ] +# CHECK-NEXT: Symbol: '' +# CHECK-NEXT: Opcode: BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB +# CHECK-NEXT: Imm: 0 +# CHECK-NEXT: ULEBExtraData: [ 0x1008 ] +# CHECK-NEXT: Symbol: '' +# CHECK-NEXT: Opcode: BIND_OPCODE_DO_BIND +# CHECK-NEXT: Imm: 0 +# CHECK-NEXT: Symbol: '' +# CHECK-NEXT: Opcode: BIND_OPCODE_DONE +# CHECK-NEXT: Imm: 0 +# CHECK-NEXT: Symbol: '' # RUN: llvm-objdump --macho --bind %t/test | FileCheck %s --check-prefix=BIND # BIND: Bind table: -# BIND-NEXT: segment section address type addend dylib symbol -# BIND-NEXT: __DATA __data {{.*}} pointer 0 libfoo _foo -# BIND-NEXT: __DATA __data {{.*}} pointer 0 libfoo _foo -# BIND-NEXT: __DATA __data {{.*}} pointer 0 libfoo _bar -# BIND-NEXT: __DATA __data {{.*}} pointer 0 libfoo _bar +# BIND-NEXT: segment section address type addend dylib symbol +# BIND-NEXT: __DATA __data 0x100001000 pointer 0 libfoo _foo +# BIND-NEXT: __DATA __data 0x100001010 pointer 0 libfoo _foo +# BIND-NEXT: __DATA __data 0x100001020 pointer 1 libfoo _foo +# BIND-NEXT: __DATA __data 0x100002030 pointer 0 libfoo _foo +# BIND-NEXT: __DATA __data 0x100001008 pointer 0 libfoo _bar +# BIND-NEXT: __DATA __data 0x100001018 pointer 0 libfoo _bar +# BIND-NEXT: __DATA __data 0x100002028 pointer 0 libfoo _bar # BIND-EMPTY: #--- foo.s @@ -39,6 +94,10 @@ .quad _bar .quad _foo .quad _bar +.quad _foo+1 +.zero 0x1000 +.quad _bar +.quad _foo .globl _main .text