Jia Liu | 9f61011 | 2012-02-17 08:55:11 +0000 | [diff] [blame] | 1 | //===-- MipsELFObjectWriter.cpp - Mips ELF Writer -------------------------===// |
Rafael Espindola | 1dc45d8 | 2011-12-22 03:03:17 +0000 | [diff] [blame] | 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 | //===----------------------------------------------------------------------===// |
| 9 | |
Akira Hatanaka | 5ba593f | 2012-03-28 00:23:33 +0000 | [diff] [blame] | 10 | #include "MCTargetDesc/MipsBaseInfo.h" |
Rafael Espindola | 1dc45d8 | 2011-12-22 03:03:17 +0000 | [diff] [blame] | 11 | #include "MCTargetDesc/MipsFixupKinds.h" |
| 12 | #include "MCTargetDesc/MipsMCTargetDesc.h" |
Akira Hatanaka | 5ba593f | 2012-03-28 00:23:33 +0000 | [diff] [blame] | 13 | #include "llvm/MC/MCAssembler.h" |
Rafael Espindola | 1dc45d8 | 2011-12-22 03:03:17 +0000 | [diff] [blame] | 14 | #include "llvm/MC/MCELFObjectWriter.h" |
| 15 | #include "llvm/MC/MCExpr.h" |
| 16 | #include "llvm/MC/MCSection.h" |
| 17 | #include "llvm/MC/MCValue.h" |
| 18 | #include "llvm/Support/ErrorHandling.h" |
Akira Hatanaka | 5ba593f | 2012-03-28 00:23:33 +0000 | [diff] [blame] | 19 | #include <list> |
Rafael Espindola | 1dc45d8 | 2011-12-22 03:03:17 +0000 | [diff] [blame] | 20 | |
| 21 | using namespace llvm; |
| 22 | |
| 23 | namespace { |
Akira Hatanaka | 5ba593f | 2012-03-28 00:23:33 +0000 | [diff] [blame] | 24 | struct RelEntry { |
| 25 | RelEntry(const ELFRelocationEntry &R, const MCSymbol *S, int64_t O) : |
| 26 | Reloc(R), Sym(S), Offset(O) {} |
| 27 | ELFRelocationEntry Reloc; |
| 28 | const MCSymbol *Sym; |
| 29 | int64_t Offset; |
| 30 | }; |
| 31 | |
| 32 | typedef std::list<RelEntry> RelLs; |
| 33 | typedef RelLs::iterator RelLsIter; |
| 34 | |
Rafael Espindola | 1dc45d8 | 2011-12-22 03:03:17 +0000 | [diff] [blame] | 35 | class MipsELFObjectWriter : public MCELFObjectTargetWriter { |
| 36 | public: |
Akira Hatanaka | 111174b | 2012-08-17 21:28:04 +0000 | [diff] [blame] | 37 | MipsELFObjectWriter(bool _is64Bit, uint8_t OSABI, |
| 38 | bool _isN64, bool IsLittleEndian); |
Rafael Espindola | 1dc45d8 | 2011-12-22 03:03:17 +0000 | [diff] [blame] | 39 | |
| 40 | virtual ~MipsELFObjectWriter(); |
| 41 | |
| 42 | virtual unsigned GetRelocType(const MCValue &Target, const MCFixup &Fixup, |
| 43 | bool IsPCRel, bool IsRelocWithSymbol, |
| 44 | int64_t Addend) const; |
| 45 | virtual unsigned getEFlags() const; |
| 46 | virtual const MCSymbol *ExplicitRelSym(const MCAssembler &Asm, |
| 47 | const MCValue &Target, |
| 48 | const MCFragment &F, |
| 49 | const MCFixup &Fixup, |
| 50 | bool IsPCRel) const; |
Akira Hatanaka | 5ba593f | 2012-03-28 00:23:33 +0000 | [diff] [blame] | 51 | virtual void sortRelocs(const MCAssembler &Asm, |
| 52 | std::vector<ELFRelocationEntry> &Relocs); |
Rafael Espindola | 1dc45d8 | 2011-12-22 03:03:17 +0000 | [diff] [blame] | 53 | }; |
| 54 | } |
| 55 | |
Jack Carter | 8ad0c27 | 2012-06-27 22:28:30 +0000 | [diff] [blame] | 56 | MipsELFObjectWriter::MipsELFObjectWriter(bool _is64Bit, uint8_t OSABI, |
Akira Hatanaka | 111174b | 2012-08-17 21:28:04 +0000 | [diff] [blame] | 57 | bool _isN64, bool IsLittleEndian) |
Akira Hatanaka | b1f68f9 | 2012-04-02 19:25:22 +0000 | [diff] [blame] | 58 | : MCELFObjectTargetWriter(_is64Bit, OSABI, ELF::EM_MIPS, |
Jack Carter | 77064c0 | 2012-08-22 00:49:30 +0000 | [diff] [blame] | 59 | /*HasRelocationAddend*/ (_isN64) ? true : false, |
Jack Carter | 8ad0c27 | 2012-06-27 22:28:30 +0000 | [diff] [blame] | 60 | /*IsN64*/ _isN64) {} |
Rafael Espindola | 1dc45d8 | 2011-12-22 03:03:17 +0000 | [diff] [blame] | 61 | |
| 62 | MipsELFObjectWriter::~MipsELFObjectWriter() {} |
| 63 | |
Akira Hatanaka | b1f68f9 | 2012-04-02 19:25:22 +0000 | [diff] [blame] | 64 | // FIXME: get the real EABI Version from the Subtarget class. |
Rafael Espindola | 1dc45d8 | 2011-12-22 03:03:17 +0000 | [diff] [blame] | 65 | unsigned MipsELFObjectWriter::getEFlags() const { |
Akira Hatanaka | b1f68f9 | 2012-04-02 19:25:22 +0000 | [diff] [blame] | 66 | |
| 67 | // FIXME: We can't tell if we are PIC (dynamic) or CPIC (static) |
| 68 | unsigned Flag = ELF::EF_MIPS_NOREORDER; |
| 69 | |
| 70 | if (is64Bit()) |
| 71 | Flag |= ELF::EF_MIPS_ARCH_64R2; |
| 72 | else |
| 73 | Flag |= ELF::EF_MIPS_ARCH_32R2; |
| 74 | return Flag; |
Rafael Espindola | 1dc45d8 | 2011-12-22 03:03:17 +0000 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | const MCSymbol *MipsELFObjectWriter::ExplicitRelSym(const MCAssembler &Asm, |
| 78 | const MCValue &Target, |
| 79 | const MCFragment &F, |
| 80 | const MCFixup &Fixup, |
| 81 | bool IsPCRel) const { |
| 82 | assert(Target.getSymA() && "SymA cannot be 0."); |
James Molloy | b47489d | 2012-01-28 15:58:32 +0000 | [diff] [blame] | 83 | const MCSymbol &Sym = Target.getSymA()->getSymbol().AliasedSymbol(); |
Rafael Espindola | 1dc45d8 | 2011-12-22 03:03:17 +0000 | [diff] [blame] | 84 | |
| 85 | if (Sym.getSection().getKind().isMergeableCString() || |
| 86 | Sym.getSection().getKind().isMergeableConst()) |
| 87 | return &Sym; |
| 88 | |
| 89 | return NULL; |
| 90 | } |
| 91 | |
| 92 | unsigned MipsELFObjectWriter::GetRelocType(const MCValue &Target, |
| 93 | const MCFixup &Fixup, |
| 94 | bool IsPCRel, |
| 95 | bool IsRelocWithSymbol, |
| 96 | int64_t Addend) const { |
| 97 | // determine the type of the relocation |
| 98 | unsigned Type = (unsigned)ELF::R_MIPS_NONE; |
| 99 | unsigned Kind = (unsigned)Fixup.getKind(); |
| 100 | |
| 101 | switch (Kind) { |
| 102 | default: |
| 103 | llvm_unreachable("invalid fixup kind!"); |
| 104 | case FK_Data_4: |
| 105 | Type = ELF::R_MIPS_32; |
| 106 | break; |
Jack Carter | 4c58381 | 2012-08-07 00:01:14 +0000 | [diff] [blame] | 107 | case FK_Data_8: |
| 108 | Type = ELF::R_MIPS_64; |
| 109 | break; |
Rafael Espindola | 1dc45d8 | 2011-12-22 03:03:17 +0000 | [diff] [blame] | 110 | case FK_GPRel_4: |
Jack Carter | f238510 | 2013-01-15 01:08:02 +0000 | [diff] [blame^] | 111 | if (isN64()) { |
| 112 | Type = setRType((unsigned)ELF::R_MIPS_GPREL32, Type); |
| 113 | Type = setRType2((unsigned)ELF::R_MIPS_64, Type); |
| 114 | Type = setRType3((unsigned)ELF::R_MIPS_NONE, Type); |
| 115 | } |
| 116 | else |
| 117 | Type = ELF::R_MIPS_GPREL32; |
Rafael Espindola | 1dc45d8 | 2011-12-22 03:03:17 +0000 | [diff] [blame] | 118 | break; |
| 119 | case Mips::fixup_Mips_GPREL16: |
| 120 | Type = ELF::R_MIPS_GPREL16; |
| 121 | break; |
| 122 | case Mips::fixup_Mips_26: |
| 123 | Type = ELF::R_MIPS_26; |
| 124 | break; |
| 125 | case Mips::fixup_Mips_CALL16: |
| 126 | Type = ELF::R_MIPS_CALL16; |
| 127 | break; |
| 128 | case Mips::fixup_Mips_GOT_Global: |
| 129 | case Mips::fixup_Mips_GOT_Local: |
| 130 | Type = ELF::R_MIPS_GOT16; |
| 131 | break; |
| 132 | case Mips::fixup_Mips_HI16: |
| 133 | Type = ELF::R_MIPS_HI16; |
| 134 | break; |
| 135 | case Mips::fixup_Mips_LO16: |
| 136 | Type = ELF::R_MIPS_LO16; |
| 137 | break; |
| 138 | case Mips::fixup_Mips_TLSGD: |
| 139 | Type = ELF::R_MIPS_TLS_GD; |
| 140 | break; |
| 141 | case Mips::fixup_Mips_GOTTPREL: |
| 142 | Type = ELF::R_MIPS_TLS_GOTTPREL; |
| 143 | break; |
| 144 | case Mips::fixup_Mips_TPREL_HI: |
| 145 | Type = ELF::R_MIPS_TLS_TPREL_HI16; |
| 146 | break; |
| 147 | case Mips::fixup_Mips_TPREL_LO: |
| 148 | Type = ELF::R_MIPS_TLS_TPREL_LO16; |
| 149 | break; |
| 150 | case Mips::fixup_Mips_TLSLDM: |
| 151 | Type = ELF::R_MIPS_TLS_LDM; |
| 152 | break; |
| 153 | case Mips::fixup_Mips_DTPREL_HI: |
| 154 | Type = ELF::R_MIPS_TLS_DTPREL_HI16; |
| 155 | break; |
| 156 | case Mips::fixup_Mips_DTPREL_LO: |
| 157 | Type = ELF::R_MIPS_TLS_DTPREL_LO16; |
| 158 | break; |
| 159 | case Mips::fixup_Mips_Branch_PCRel: |
| 160 | case Mips::fixup_Mips_PC16: |
| 161 | Type = ELF::R_MIPS_PC16; |
| 162 | break; |
Jack Carter | b9f9de9 | 2012-06-27 22:48:25 +0000 | [diff] [blame] | 163 | case Mips::fixup_Mips_GOT_PAGE: |
| 164 | Type = ELF::R_MIPS_GOT_PAGE; |
| 165 | break; |
| 166 | case Mips::fixup_Mips_GOT_OFST: |
| 167 | Type = ELF::R_MIPS_GOT_OFST; |
| 168 | break; |
Jack Carter | 5ddcfda | 2012-07-13 19:15:47 +0000 | [diff] [blame] | 169 | case Mips::fixup_Mips_GOT_DISP: |
| 170 | Type = ELF::R_MIPS_GOT_DISP; |
| 171 | break; |
Jack Carter | b9f9de9 | 2012-06-27 22:48:25 +0000 | [diff] [blame] | 172 | case Mips::fixup_Mips_GPOFF_HI: |
| 173 | Type = setRType((unsigned)ELF::R_MIPS_GPREL16, Type); |
| 174 | Type = setRType2((unsigned)ELF::R_MIPS_SUB, Type); |
| 175 | Type = setRType3((unsigned)ELF::R_MIPS_HI16, Type); |
| 176 | break; |
| 177 | case Mips::fixup_Mips_GPOFF_LO: |
| 178 | Type = setRType((unsigned)ELF::R_MIPS_GPREL16, Type); |
| 179 | Type = setRType2((unsigned)ELF::R_MIPS_SUB, Type); |
| 180 | Type = setRType3((unsigned)ELF::R_MIPS_LO16, Type); |
| 181 | break; |
Jack Carter | 84491ab | 2012-08-06 21:26:03 +0000 | [diff] [blame] | 182 | case Mips::fixup_Mips_HIGHER: |
| 183 | Type = ELF::R_MIPS_HIGHER; |
| 184 | break; |
| 185 | case Mips::fixup_Mips_HIGHEST: |
| 186 | Type = ELF::R_MIPS_HIGHEST; |
| 187 | break; |
Jack Carter | b05cb67 | 2012-11-21 23:38:59 +0000 | [diff] [blame] | 188 | case Mips::fixup_Mips_GOT_HI16: |
| 189 | Type = ELF::R_MIPS_GOT_HI16; |
| 190 | break; |
| 191 | case Mips::fixup_Mips_GOT_LO16: |
| 192 | Type = ELF::R_MIPS_GOT_LO16; |
| 193 | break; |
| 194 | case Mips::fixup_Mips_CALL_HI16: |
| 195 | Type = ELF::R_MIPS_CALL_HI16; |
| 196 | break; |
| 197 | case Mips::fixup_Mips_CALL_LO16: |
| 198 | Type = ELF::R_MIPS_CALL_LO16; |
| 199 | break; |
Rafael Espindola | 1dc45d8 | 2011-12-22 03:03:17 +0000 | [diff] [blame] | 200 | } |
Rafael Espindola | 1dc45d8 | 2011-12-22 03:03:17 +0000 | [diff] [blame] | 201 | return Type; |
| 202 | } |
| 203 | |
Akira Hatanaka | 5ba593f | 2012-03-28 00:23:33 +0000 | [diff] [blame] | 204 | // Return true if R is either a GOT16 against a local symbol or HI16. |
| 205 | static bool NeedsMatchingLo(const MCAssembler &Asm, const RelEntry &R) { |
| 206 | if (!R.Sym) |
| 207 | return false; |
| 208 | |
| 209 | MCSymbolData &SD = Asm.getSymbolData(R.Sym->AliasedSymbol()); |
| 210 | |
| 211 | return ((R.Reloc.Type == ELF::R_MIPS_GOT16) && !SD.isExternal()) || |
| 212 | (R.Reloc.Type == ELF::R_MIPS_HI16); |
| 213 | } |
| 214 | |
| 215 | static bool HasMatchingLo(const MCAssembler &Asm, RelLsIter I, RelLsIter Last) { |
| 216 | if (I == Last) |
| 217 | return false; |
| 218 | |
| 219 | RelLsIter Hi = I++; |
| 220 | |
| 221 | return (I->Reloc.Type == ELF::R_MIPS_LO16) && (Hi->Sym == I->Sym) && |
| 222 | (Hi->Offset == I->Offset); |
| 223 | } |
| 224 | |
| 225 | static bool HasSameSymbol(const RelEntry &R0, const RelEntry &R1) { |
| 226 | return R0.Sym == R1.Sym; |
| 227 | } |
| 228 | |
| 229 | static int CompareOffset(const RelEntry &R0, const RelEntry &R1) { |
| 230 | return (R0.Offset > R1.Offset) ? 1 : ((R0.Offset == R1.Offset) ? 0 : -1); |
| 231 | } |
| 232 | |
| 233 | void MipsELFObjectWriter::sortRelocs(const MCAssembler &Asm, |
| 234 | std::vector<ELFRelocationEntry> &Relocs) { |
Jack Carter | 8ad0c27 | 2012-06-27 22:28:30 +0000 | [diff] [blame] | 235 | // Call the default function first. Relocations are sorted in descending |
Akira Hatanaka | 5ba593f | 2012-03-28 00:23:33 +0000 | [diff] [blame] | 236 | // order of r_offset. |
| 237 | MCELFObjectTargetWriter::sortRelocs(Asm, Relocs); |
Akira Hatanaka | 5fd2248 | 2012-06-14 21:10:56 +0000 | [diff] [blame] | 238 | |
Akira Hatanaka | 5ba593f | 2012-03-28 00:23:33 +0000 | [diff] [blame] | 239 | RelLs RelocLs; |
| 240 | std::vector<RelLsIter> Unmatched; |
| 241 | |
| 242 | // Fill RelocLs. Traverse Relocs backwards so that relocations in RelocLs |
| 243 | // are in ascending order of r_offset. |
| 244 | for (std::vector<ELFRelocationEntry>::reverse_iterator R = Relocs.rbegin(); |
| 245 | R != Relocs.rend(); ++R) { |
| 246 | std::pair<const MCSymbolRefExpr*, int64_t> P = |
| 247 | MipsGetSymAndOffset(*R->Fixup); |
| 248 | RelocLs.push_back(RelEntry(*R, P.first ? &P.first->getSymbol() : 0, |
| 249 | P.second)); |
| 250 | } |
| 251 | |
| 252 | // Get list of unmatched HI16 and GOT16. |
| 253 | for (RelLsIter R = RelocLs.begin(); R != RelocLs.end(); ++R) |
| 254 | if (NeedsMatchingLo(Asm, *R) && !HasMatchingLo(Asm, R, --RelocLs.end())) |
| 255 | Unmatched.push_back(R); |
| 256 | |
| 257 | // Insert unmatched HI16 and GOT16 immediately before their matching LO16. |
| 258 | for (std::vector<RelLsIter>::iterator U = Unmatched.begin(); |
| 259 | U != Unmatched.end(); ++U) { |
| 260 | RelLsIter LoPos = RelocLs.end(), HiPos = *U; |
| 261 | bool MatchedLo = false; |
| 262 | |
| 263 | for (RelLsIter R = RelocLs.begin(); R != RelocLs.end(); ++R) { |
| 264 | if ((R->Reloc.Type == ELF::R_MIPS_LO16) && HasSameSymbol(*HiPos, *R) && |
| 265 | (CompareOffset(*R, *HiPos) >= 0) && |
| 266 | ((LoPos == RelocLs.end()) || ((CompareOffset(*R, *LoPos) < 0)) || |
| 267 | (!MatchedLo && !CompareOffset(*R, *LoPos)))) |
| 268 | LoPos = R; |
| 269 | |
| 270 | MatchedLo = NeedsMatchingLo(Asm, *R) && |
| 271 | HasMatchingLo(Asm, R, --RelocLs.end()); |
| 272 | } |
| 273 | |
| 274 | // If a matching LoPos was found, move HiPos and insert it before LoPos. |
| 275 | // Make the offsets of HiPos and LoPos match. |
| 276 | if (LoPos != RelocLs.end()) { |
| 277 | HiPos->Offset = LoPos->Offset; |
| 278 | RelocLs.insert(LoPos, *HiPos); |
| 279 | RelocLs.erase(HiPos); |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | // Put the sorted list back in reverse order. |
| 284 | assert(Relocs.size() == RelocLs.size()); |
| 285 | unsigned I = RelocLs.size(); |
| 286 | |
| 287 | for (RelLsIter R = RelocLs.begin(); R != RelocLs.end(); ++R) |
| 288 | Relocs[--I] = R->Reloc; |
| 289 | } |
| 290 | |
Akira Hatanaka | b1f68f9 | 2012-04-02 19:25:22 +0000 | [diff] [blame] | 291 | MCObjectWriter *llvm::createMipsELFObjectWriter(raw_ostream &OS, |
| 292 | uint8_t OSABI, |
| 293 | bool IsLittleEndian, |
| 294 | bool Is64Bit) { |
Jack Carter | 8ad0c27 | 2012-06-27 22:28:30 +0000 | [diff] [blame] | 295 | MCELFObjectTargetWriter *MOTW = new MipsELFObjectWriter(Is64Bit, OSABI, |
Akira Hatanaka | 111174b | 2012-08-17 21:28:04 +0000 | [diff] [blame] | 296 | (Is64Bit) ? true : false, |
| 297 | IsLittleEndian); |
Rafael Espindola | 1dc45d8 | 2011-12-22 03:03:17 +0000 | [diff] [blame] | 298 | return createELFObjectWriter(MOTW, OS, IsLittleEndian); |
| 299 | } |