Deleted Added
full compact
MachObjectWriter.cpp (208954) MachObjectWriter.cpp (210299)
1//===- lib/MC/MachObjectWriter.cpp - Mach-O File Writer -------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//

--- 19 unchanged lines hidden (view full) ---

28#include <vector>
29using namespace llvm;
30
31static unsigned getFixupKindLog2Size(unsigned Kind) {
32 switch (Kind) {
33 default: llvm_unreachable("invalid fixup kind!");
34 case X86::reloc_pcrel_1byte:
35 case FK_Data_1: return 0;
1//===- lib/MC/MachObjectWriter.cpp - Mach-O File Writer -------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//

--- 19 unchanged lines hidden (view full) ---

28#include <vector>
29using namespace llvm;
30
31static unsigned getFixupKindLog2Size(unsigned Kind) {
32 switch (Kind) {
33 default: llvm_unreachable("invalid fixup kind!");
34 case X86::reloc_pcrel_1byte:
35 case FK_Data_1: return 0;
36 case X86::reloc_pcrel_2byte:
36 case FK_Data_2: return 1;
37 case X86::reloc_pcrel_4byte:
38 case X86::reloc_riprel_4byte:
39 case X86::reloc_riprel_4byte_movq_load:
40 case FK_Data_4: return 2;
41 case FK_Data_8: return 3;
42 }
43}
44
45static bool isFixupKindPCRel(unsigned Kind) {
46 switch (Kind) {
47 default:
48 return false;
49 case X86::reloc_pcrel_1byte:
37 case FK_Data_2: return 1;
38 case X86::reloc_pcrel_4byte:
39 case X86::reloc_riprel_4byte:
40 case X86::reloc_riprel_4byte_movq_load:
41 case FK_Data_4: return 2;
42 case FK_Data_8: return 3;
43 }
44}
45
46static bool isFixupKindPCRel(unsigned Kind) {
47 switch (Kind) {
48 default:
49 return false;
50 case X86::reloc_pcrel_1byte:
51 case X86::reloc_pcrel_2byte:
50 case X86::reloc_pcrel_4byte:
51 case X86::reloc_riprel_4byte:
52 case X86::reloc_riprel_4byte_movq_load:
53 return true;
54 }
55}
56
57static bool isFixupKindRIPRel(unsigned Kind) {

--- 675 unchanged lines hidden (view full) ---

733 (Type << 24) |
734 (Log2Size << 28) |
735 (IsPCRel << 30) |
736 RF_Scattered);
737 MRE.Word1 = Value;
738 Relocations[Fragment->getParent()].push_back(MRE);
739 }
740
52 case X86::reloc_pcrel_4byte:
53 case X86::reloc_riprel_4byte:
54 case X86::reloc_riprel_4byte_movq_load:
55 return true;
56 }
57}
58
59static bool isFixupKindRIPRel(unsigned Kind) {

--- 675 unchanged lines hidden (view full) ---

735 (Type << 24) |
736 (Log2Size << 28) |
737 (IsPCRel << 30) |
738 RF_Scattered);
739 MRE.Word1 = Value;
740 Relocations[Fragment->getParent()].push_back(MRE);
741 }
742
743 void RecordTLVPRelocation(const MCAssembler &Asm,
744 const MCAsmLayout &Layout,
745 const MCFragment *Fragment,
746 const MCFixup &Fixup, MCValue Target,
747 uint64_t &FixedValue) {
748 assert(Target.getSymA()->getKind() == MCSymbolRefExpr::VK_TLVP &&
749 !Is64Bit &&
750 "Should only be called with a 32-bit TLVP relocation!");
751
752 unsigned Log2Size = getFixupKindLog2Size(Fixup.getKind());
753 uint32_t Value = Layout.getFragmentOffset(Fragment)+Fixup.getOffset();
754 unsigned IsPCRel = 0;
755
756 // Get the symbol data.
757 MCSymbolData *SD_A = &Asm.getSymbolData(Target.getSymA()->getSymbol());
758 unsigned Index = SD_A->getIndex();
759
760 // We're only going to have a second symbol in pic mode and it'll be a
761 // subtraction from the picbase. For 32-bit pic the addend is the difference
762 // between the picbase and the next address. For 32-bit static the addend
763 // is zero.
764 if (Target.getSymB()) {
765 // If this is a subtraction then we're pcrel.
766 uint32_t FixupAddress =
767 Layout.getFragmentAddress(Fragment) + Fixup.getOffset();
768 MCSymbolData *SD_B = &Asm.getSymbolData(Target.getSymB()->getSymbol());
769 IsPCRel = 1;
770 FixedValue = (FixupAddress - Layout.getSymbolAddress(SD_B) +
771 Target.getConstant());
772 FixedValue += 1 << Log2Size;
773 } else {
774 FixedValue = 0;
775 }
776
777 // struct relocation_info (8 bytes)
778 MachRelocationEntry MRE;
779 MRE.Word0 = Value;
780 MRE.Word1 = ((Index << 0) |
781 (IsPCRel << 24) |
782 (Log2Size << 25) |
783 (1 << 27) | // Extern
784 (RIT_TLV << 28)); // Type
785 Relocations[Fragment->getParent()].push_back(MRE);
786 }
787
741 void RecordRelocation(const MCAssembler &Asm, const MCAsmLayout &Layout,
742 const MCFragment *Fragment, const MCFixup &Fixup,
743 MCValue Target, uint64_t &FixedValue) {
744 if (Is64Bit) {
745 RecordX86_64Relocation(Asm, Layout, Fragment, Fixup, Target, FixedValue);
746 return;
747 }
748
749 unsigned IsPCRel = isFixupKindPCRel(Fixup.getKind());
750 unsigned Log2Size = getFixupKindLog2Size(Fixup.getKind());
751
788 void RecordRelocation(const MCAssembler &Asm, const MCAsmLayout &Layout,
789 const MCFragment *Fragment, const MCFixup &Fixup,
790 MCValue Target, uint64_t &FixedValue) {
791 if (Is64Bit) {
792 RecordX86_64Relocation(Asm, Layout, Fragment, Fixup, Target, FixedValue);
793 return;
794 }
795
796 unsigned IsPCRel = isFixupKindPCRel(Fixup.getKind());
797 unsigned Log2Size = getFixupKindLog2Size(Fixup.getKind());
798
799 // If this is a 32-bit TLVP reloc it's handled a bit differently.
800 if (Target.getSymA()->getKind() == MCSymbolRefExpr::VK_TLVP) {
801 RecordTLVPRelocation(Asm, Layout, Fragment, Fixup, Target, FixedValue);
802 return;
803 }
804
752 // If this is a difference or a defined symbol plus an offset, then we need
753 // a scattered relocation entry.
754 // Differences always require scattered relocations.
755 if (Target.getSymB())
756 return RecordScatteredRelocation(Asm, Layout, Fragment, Fixup,
757 Target, FixedValue);
758
759 // Get the symbol data, if any.

--- 7 unchanged lines hidden (view full) ---

767 if (IsPCRel)
768 Offset += 1 << Log2Size;
769 if (Offset && SD && !doesSymbolRequireExternRelocation(SD))
770 return RecordScatteredRelocation(Asm, Layout, Fragment, Fixup,
771 Target, FixedValue);
772
773 // See <reloc.h>.
774 uint32_t FixupOffset = Layout.getFragmentOffset(Fragment)+Fixup.getOffset();
805 // If this is a difference or a defined symbol plus an offset, then we need
806 // a scattered relocation entry.
807 // Differences always require scattered relocations.
808 if (Target.getSymB())
809 return RecordScatteredRelocation(Asm, Layout, Fragment, Fixup,
810 Target, FixedValue);
811
812 // Get the symbol data, if any.

--- 7 unchanged lines hidden (view full) ---

820 if (IsPCRel)
821 Offset += 1 << Log2Size;
822 if (Offset && SD && !doesSymbolRequireExternRelocation(SD))
823 return RecordScatteredRelocation(Asm, Layout, Fragment, Fixup,
824 Target, FixedValue);
825
826 // See <reloc.h>.
827 uint32_t FixupOffset = Layout.getFragmentOffset(Fragment)+Fixup.getOffset();
775 uint32_t Value = 0;
776 unsigned Index = 0;
777 unsigned IsExtern = 0;
778 unsigned Type = 0;
779
780 if (Target.isAbsolute()) { // constant
781 // SymbolNum of 0 indicates the absolute section.
782 //
783 // FIXME: Currently, these are never generated (see code below). I cannot
784 // find a case where they are actually emitted.
785 Type = RIT_Vanilla;
828 unsigned Index = 0;
829 unsigned IsExtern = 0;
830 unsigned Type = 0;
831
832 if (Target.isAbsolute()) { // constant
833 // SymbolNum of 0 indicates the absolute section.
834 //
835 // FIXME: Currently, these are never generated (see code below). I cannot
836 // find a case where they are actually emitted.
837 Type = RIT_Vanilla;
786 Value = 0;
787 } else {
788 // Check whether we need an external or internal relocation.
789 if (doesSymbolRequireExternRelocation(SD)) {
790 IsExtern = 1;
791 Index = SD->getIndex();
792 // For external relocations, make sure to offset the fixup value to
793 // compensate for the addend of the symbol address, if it was
794 // undefined. This occurs with weak definitions, for example.
795 if (!SD->Symbol->isUndefined())
796 FixedValue -= Layout.getSymbolAddress(SD);
838 } else {
839 // Check whether we need an external or internal relocation.
840 if (doesSymbolRequireExternRelocation(SD)) {
841 IsExtern = 1;
842 Index = SD->getIndex();
843 // For external relocations, make sure to offset the fixup value to
844 // compensate for the addend of the symbol address, if it was
845 // undefined. This occurs with weak definitions, for example.
846 if (!SD->Symbol->isUndefined())
847 FixedValue -= Layout.getSymbolAddress(SD);
797 Value = 0;
798 } else {
799 // The index is the section ordinal (1-based).
800 Index = SD->getFragment()->getParent()->getOrdinal() + 1;
848 } else {
849 // The index is the section ordinal (1-based).
850 Index = SD->getFragment()->getParent()->getOrdinal() + 1;
801 Value = Layout.getSymbolAddress(SD);
802 }
803
804 Type = RIT_Vanilla;
805 }
806
807 // struct relocation_info (8 bytes)
808 MachRelocationEntry MRE;
809 MRE.Word0 = FixupOffset;

--- 83 unchanged lines hidden (view full) ---

893 // table, then sort the symbols is chosen to match 'as'. Even though it
894 // doesn't matter for correctness, this is important for letting us diff .o
895 // files.
896 for (MCAssembler::symbol_iterator it = Asm.symbol_begin(),
897 ie = Asm.symbol_end(); it != ie; ++it) {
898 const MCSymbol &Symbol = it->getSymbol();
899
900 // Ignore non-linker visible symbols.
851 }
852
853 Type = RIT_Vanilla;
854 }
855
856 // struct relocation_info (8 bytes)
857 MachRelocationEntry MRE;
858 MRE.Word0 = FixupOffset;

--- 83 unchanged lines hidden (view full) ---

942 // table, then sort the symbols is chosen to match 'as'. Even though it
943 // doesn't matter for correctness, this is important for letting us diff .o
944 // files.
945 for (MCAssembler::symbol_iterator it = Asm.symbol_begin(),
946 ie = Asm.symbol_end(); it != ie; ++it) {
947 const MCSymbol &Symbol = it->getSymbol();
948
949 // Ignore non-linker visible symbols.
901 if (!Asm.isSymbolLinkerVisible(it))
950 if (!Asm.isSymbolLinkerVisible(it->getSymbol()))
902 continue;
903
904 if (!it->isExternal() && !Symbol.isUndefined())
905 continue;
906
907 uint64_t &Entry = StringIndexMap[Symbol.getName()];
908 if (!Entry) {
909 Entry = StringTable.size();

--- 19 unchanged lines hidden (view full) ---

929 }
930
931 // Now add the data for local symbols.
932 for (MCAssembler::symbol_iterator it = Asm.symbol_begin(),
933 ie = Asm.symbol_end(); it != ie; ++it) {
934 const MCSymbol &Symbol = it->getSymbol();
935
936 // Ignore non-linker visible symbols.
951 continue;
952
953 if (!it->isExternal() && !Symbol.isUndefined())
954 continue;
955
956 uint64_t &Entry = StringIndexMap[Symbol.getName()];
957 if (!Entry) {
958 Entry = StringTable.size();

--- 19 unchanged lines hidden (view full) ---

978 }
979
980 // Now add the data for local symbols.
981 for (MCAssembler::symbol_iterator it = Asm.symbol_begin(),
982 ie = Asm.symbol_end(); it != ie; ++it) {
983 const MCSymbol &Symbol = it->getSymbol();
984
985 // Ignore non-linker visible symbols.
937 if (!Asm.isSymbolLinkerVisible(it))
986 if (!Asm.isSymbolLinkerVisible(it->getSymbol()))
938 continue;
939
940 if (it->isExternal() || Symbol.isUndefined())
941 continue;
942
943 uint64_t &Entry = StringIndexMap[Symbol.getName()];
944 if (!Entry) {
945 Entry = StringTable.size();

--- 235 unchanged lines hidden ---
987 continue;
988
989 if (it->isExternal() || Symbol.isUndefined())
990 continue;
991
992 uint64_t &Entry = StringIndexMap[Symbol.getName()];
993 if (!Entry) {
994 Entry = StringTable.size();

--- 235 unchanged lines hidden ---