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" |
Petar Jovanovic | 0380d0b | 2015-04-14 13:23:34 +0000 | [diff] [blame] | 13 | #include "llvm/ADT/STLExtras.h" |
Akira Hatanaka | 5ba593f | 2012-03-28 00:23:33 +0000 | [diff] [blame] | 14 | #include "llvm/MC/MCAssembler.h" |
Zoran Jovanovic | 1064691 | 2014-12-30 22:04:16 +0000 | [diff] [blame] | 15 | #include "llvm/MC/MCELF.h" |
Rafael Espindola | 1dc45d8 | 2011-12-22 03:03:17 +0000 | [diff] [blame] | 16 | #include "llvm/MC/MCELFObjectWriter.h" |
| 17 | #include "llvm/MC/MCExpr.h" |
| 18 | #include "llvm/MC/MCSection.h" |
| 19 | #include "llvm/MC/MCValue.h" |
| 20 | #include "llvm/Support/ErrorHandling.h" |
Akira Hatanaka | 5ba593f | 2012-03-28 00:23:33 +0000 | [diff] [blame] | 21 | #include <list> |
Rafael Espindola | 1dc45d8 | 2011-12-22 03:03:17 +0000 | [diff] [blame] | 22 | |
| 23 | using namespace llvm; |
| 24 | |
| 25 | namespace { |
Petar Jovanovic | 0380d0b | 2015-04-14 13:23:34 +0000 | [diff] [blame] | 26 | // A helper structure based on ELFRelocationEntry, used for sorting entries in |
| 27 | // the relocation table. |
| 28 | struct MipsRelocationEntry { |
| 29 | MipsRelocationEntry(const ELFRelocationEntry &R) |
| 30 | : R(R), SortOffset(R.Offset), HasMatchingHi(false) {} |
| 31 | const ELFRelocationEntry R; |
| 32 | // SortOffset equals R.Offset except for the *HI16 relocations, for which it |
| 33 | // will be set based on the R.Offset of the matching *LO16 relocation. |
| 34 | int64_t SortOffset; |
| 35 | // True when this is a *LO16 relocation chosen as a match for a *HI16 |
| 36 | // relocation. |
| 37 | bool HasMatchingHi; |
| 38 | }; |
| 39 | |
Rafael Espindola | 1dc45d8 | 2011-12-22 03:03:17 +0000 | [diff] [blame] | 40 | class MipsELFObjectWriter : public MCELFObjectTargetWriter { |
| 41 | public: |
Akira Hatanaka | 111174b | 2012-08-17 21:28:04 +0000 | [diff] [blame] | 42 | MipsELFObjectWriter(bool _is64Bit, uint8_t OSABI, |
| 43 | bool _isN64, bool IsLittleEndian); |
Rafael Espindola | 1dc45d8 | 2011-12-22 03:03:17 +0000 | [diff] [blame] | 44 | |
Alexander Kornienko | f817c1c | 2015-04-11 02:11:45 +0000 | [diff] [blame] | 45 | ~MipsELFObjectWriter() override; |
Rafael Espindola | 1dc45d8 | 2011-12-22 03:03:17 +0000 | [diff] [blame] | 46 | |
Rafael Espindola | 9ab3801 | 2014-03-27 20:41:17 +0000 | [diff] [blame] | 47 | unsigned GetRelocType(const MCValue &Target, const MCFixup &Fixup, |
Rafael Espindola | c03f44c | 2014-03-27 20:49:35 +0000 | [diff] [blame] | 48 | bool IsPCRel) const override; |
Rafael Espindola | ece40ca | 2015-05-29 18:26:09 +0000 | [diff] [blame] | 49 | bool needsRelocateWithSymbol(const MCSymbol &Sym, |
Ulrich Weigand | 46797c6 | 2014-07-20 23:15:06 +0000 | [diff] [blame] | 50 | unsigned Type) const override; |
Petar Jovanovic | 0380d0b | 2015-04-14 13:23:34 +0000 | [diff] [blame] | 51 | virtual void sortRelocs(const MCAssembler &Asm, |
| 52 | std::vector<ELFRelocationEntry> &Relocs) override; |
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) |
Simon Atanasyan | c99ce68 | 2015-03-24 12:24:56 +0000 | [diff] [blame] | 58 | : MCELFObjectTargetWriter(_is64Bit, OSABI, ELF::EM_MIPS, |
| 59 | /*HasRelocationAddend*/ _isN64, |
| 60 | /*IsN64*/ _isN64) {} |
Rafael Espindola | 1dc45d8 | 2011-12-22 03:03:17 +0000 | [diff] [blame] | 61 | |
| 62 | MipsELFObjectWriter::~MipsELFObjectWriter() {} |
| 63 | |
Rafael Espindola | 1dc45d8 | 2011-12-22 03:03:17 +0000 | [diff] [blame] | 64 | unsigned MipsELFObjectWriter::GetRelocType(const MCValue &Target, |
| 65 | const MCFixup &Fixup, |
Rafael Espindola | c03f44c | 2014-03-27 20:49:35 +0000 | [diff] [blame] | 66 | bool IsPCRel) const { |
Rafael Espindola | 1dc45d8 | 2011-12-22 03:03:17 +0000 | [diff] [blame] | 67 | // determine the type of the relocation |
| 68 | unsigned Type = (unsigned)ELF::R_MIPS_NONE; |
| 69 | unsigned Kind = (unsigned)Fixup.getKind(); |
| 70 | |
| 71 | switch (Kind) { |
| 72 | default: |
| 73 | llvm_unreachable("invalid fixup kind!"); |
Daniel Sanders | 60f1db0 | 2015-03-13 12:45:09 +0000 | [diff] [blame] | 74 | case Mips::fixup_Mips_32: |
Rafael Espindola | 1dc45d8 | 2011-12-22 03:03:17 +0000 | [diff] [blame] | 75 | case FK_Data_4: |
| 76 | Type = ELF::R_MIPS_32; |
| 77 | break; |
Daniel Sanders | 60f1db0 | 2015-03-13 12:45:09 +0000 | [diff] [blame] | 78 | case Mips::fixup_Mips_64: |
Jack Carter | 4c58381 | 2012-08-07 00:01:14 +0000 | [diff] [blame] | 79 | case FK_Data_8: |
| 80 | Type = ELF::R_MIPS_64; |
| 81 | break; |
Rafael Espindola | 1dc45d8 | 2011-12-22 03:03:17 +0000 | [diff] [blame] | 82 | case FK_GPRel_4: |
Jack Carter | f238510 | 2013-01-15 01:08:02 +0000 | [diff] [blame] | 83 | if (isN64()) { |
| 84 | Type = setRType((unsigned)ELF::R_MIPS_GPREL32, Type); |
| 85 | Type = setRType2((unsigned)ELF::R_MIPS_64, Type); |
| 86 | Type = setRType3((unsigned)ELF::R_MIPS_NONE, Type); |
| 87 | } |
| 88 | else |
| 89 | Type = ELF::R_MIPS_GPREL32; |
Rafael Espindola | 1dc45d8 | 2011-12-22 03:03:17 +0000 | [diff] [blame] | 90 | break; |
| 91 | case Mips::fixup_Mips_GPREL16: |
| 92 | Type = ELF::R_MIPS_GPREL16; |
| 93 | break; |
| 94 | case Mips::fixup_Mips_26: |
| 95 | Type = ELF::R_MIPS_26; |
| 96 | break; |
| 97 | case Mips::fixup_Mips_CALL16: |
| 98 | Type = ELF::R_MIPS_CALL16; |
| 99 | break; |
| 100 | case Mips::fixup_Mips_GOT_Global: |
| 101 | case Mips::fixup_Mips_GOT_Local: |
| 102 | Type = ELF::R_MIPS_GOT16; |
| 103 | break; |
| 104 | case Mips::fixup_Mips_HI16: |
| 105 | Type = ELF::R_MIPS_HI16; |
| 106 | break; |
| 107 | case Mips::fixup_Mips_LO16: |
| 108 | Type = ELF::R_MIPS_LO16; |
| 109 | break; |
| 110 | case Mips::fixup_Mips_TLSGD: |
| 111 | Type = ELF::R_MIPS_TLS_GD; |
| 112 | break; |
| 113 | case Mips::fixup_Mips_GOTTPREL: |
| 114 | Type = ELF::R_MIPS_TLS_GOTTPREL; |
| 115 | break; |
| 116 | case Mips::fixup_Mips_TPREL_HI: |
| 117 | Type = ELF::R_MIPS_TLS_TPREL_HI16; |
| 118 | break; |
| 119 | case Mips::fixup_Mips_TPREL_LO: |
| 120 | Type = ELF::R_MIPS_TLS_TPREL_LO16; |
| 121 | break; |
| 122 | case Mips::fixup_Mips_TLSLDM: |
| 123 | Type = ELF::R_MIPS_TLS_LDM; |
| 124 | break; |
| 125 | case Mips::fixup_Mips_DTPREL_HI: |
| 126 | Type = ELF::R_MIPS_TLS_DTPREL_HI16; |
| 127 | break; |
| 128 | case Mips::fixup_Mips_DTPREL_LO: |
| 129 | Type = ELF::R_MIPS_TLS_DTPREL_LO16; |
| 130 | break; |
| 131 | case Mips::fixup_Mips_Branch_PCRel: |
| 132 | case Mips::fixup_Mips_PC16: |
| 133 | Type = ELF::R_MIPS_PC16; |
| 134 | break; |
Jack Carter | b9f9de9 | 2012-06-27 22:48:25 +0000 | [diff] [blame] | 135 | case Mips::fixup_Mips_GOT_PAGE: |
| 136 | Type = ELF::R_MIPS_GOT_PAGE; |
| 137 | break; |
| 138 | case Mips::fixup_Mips_GOT_OFST: |
| 139 | Type = ELF::R_MIPS_GOT_OFST; |
| 140 | break; |
Jack Carter | 5ddcfda | 2012-07-13 19:15:47 +0000 | [diff] [blame] | 141 | case Mips::fixup_Mips_GOT_DISP: |
| 142 | Type = ELF::R_MIPS_GOT_DISP; |
| 143 | break; |
Jack Carter | b9f9de9 | 2012-06-27 22:48:25 +0000 | [diff] [blame] | 144 | case Mips::fixup_Mips_GPOFF_HI: |
| 145 | Type = setRType((unsigned)ELF::R_MIPS_GPREL16, Type); |
| 146 | Type = setRType2((unsigned)ELF::R_MIPS_SUB, Type); |
| 147 | Type = setRType3((unsigned)ELF::R_MIPS_HI16, Type); |
| 148 | break; |
| 149 | case Mips::fixup_Mips_GPOFF_LO: |
| 150 | Type = setRType((unsigned)ELF::R_MIPS_GPREL16, Type); |
| 151 | Type = setRType2((unsigned)ELF::R_MIPS_SUB, Type); |
| 152 | Type = setRType3((unsigned)ELF::R_MIPS_LO16, Type); |
| 153 | break; |
Jack Carter | 84491ab | 2012-08-06 21:26:03 +0000 | [diff] [blame] | 154 | case Mips::fixup_Mips_HIGHER: |
| 155 | Type = ELF::R_MIPS_HIGHER; |
| 156 | break; |
| 157 | case Mips::fixup_Mips_HIGHEST: |
| 158 | Type = ELF::R_MIPS_HIGHEST; |
| 159 | break; |
Jack Carter | b05cb67 | 2012-11-21 23:38:59 +0000 | [diff] [blame] | 160 | case Mips::fixup_Mips_GOT_HI16: |
| 161 | Type = ELF::R_MIPS_GOT_HI16; |
| 162 | break; |
| 163 | case Mips::fixup_Mips_GOT_LO16: |
| 164 | Type = ELF::R_MIPS_GOT_LO16; |
| 165 | break; |
| 166 | case Mips::fixup_Mips_CALL_HI16: |
| 167 | Type = ELF::R_MIPS_CALL_HI16; |
| 168 | break; |
| 169 | case Mips::fixup_Mips_CALL_LO16: |
| 170 | Type = ELF::R_MIPS_CALL_LO16; |
| 171 | break; |
Zoran Jovanovic | 507e084 | 2013-10-29 16:38:59 +0000 | [diff] [blame] | 172 | case Mips::fixup_MICROMIPS_26_S1: |
| 173 | Type = ELF::R_MICROMIPS_26_S1; |
| 174 | break; |
Zoran Jovanovic | e7ae8af | 2013-10-23 16:14:44 +0000 | [diff] [blame] | 175 | case Mips::fixup_MICROMIPS_HI16: |
| 176 | Type = ELF::R_MICROMIPS_HI16; |
| 177 | break; |
| 178 | case Mips::fixup_MICROMIPS_LO16: |
| 179 | Type = ELF::R_MICROMIPS_LO16; |
| 180 | break; |
| 181 | case Mips::fixup_MICROMIPS_GOT16: |
| 182 | Type = ELF::R_MICROMIPS_GOT16; |
| 183 | break; |
Jozef Kolek | 9761e96 | 2015-01-12 12:03:34 +0000 | [diff] [blame] | 184 | case Mips::fixup_MICROMIPS_PC7_S1: |
| 185 | Type = ELF::R_MICROMIPS_PC7_S1; |
| 186 | break; |
Jozef Kolek | 5cfebdd | 2015-01-21 12:39:30 +0000 | [diff] [blame] | 187 | case Mips::fixup_MICROMIPS_PC10_S1: |
| 188 | Type = ELF::R_MICROMIPS_PC10_S1; |
| 189 | break; |
Zoran Jovanovic | 8a80aa7 | 2013-11-04 14:53:22 +0000 | [diff] [blame] | 190 | case Mips::fixup_MICROMIPS_PC16_S1: |
| 191 | Type = ELF::R_MICROMIPS_PC16_S1; |
| 192 | break; |
Zoran Jovanovic | e7ae8af | 2013-10-23 16:14:44 +0000 | [diff] [blame] | 193 | case Mips::fixup_MICROMIPS_CALL16: |
| 194 | Type = ELF::R_MICROMIPS_CALL16; |
| 195 | break; |
| 196 | case Mips::fixup_MICROMIPS_GOT_DISP: |
| 197 | Type = ELF::R_MICROMIPS_GOT_DISP; |
| 198 | break; |
| 199 | case Mips::fixup_MICROMIPS_GOT_PAGE: |
| 200 | Type = ELF::R_MICROMIPS_GOT_PAGE; |
| 201 | break; |
| 202 | case Mips::fixup_MICROMIPS_GOT_OFST: |
| 203 | Type = ELF::R_MICROMIPS_GOT_OFST; |
| 204 | break; |
Zoran Jovanovic | 69be811 | 2013-12-19 16:02:32 +0000 | [diff] [blame] | 205 | case Mips::fixup_MICROMIPS_TLS_GD: |
| 206 | Type = ELF::R_MICROMIPS_TLS_GD; |
| 207 | break; |
| 208 | case Mips::fixup_MICROMIPS_TLS_LDM: |
| 209 | Type = ELF::R_MICROMIPS_TLS_LDM; |
| 210 | break; |
Zoran Jovanovic | e7ae8af | 2013-10-23 16:14:44 +0000 | [diff] [blame] | 211 | case Mips::fixup_MICROMIPS_TLS_DTPREL_HI16: |
| 212 | Type = ELF::R_MICROMIPS_TLS_DTPREL_HI16; |
| 213 | break; |
| 214 | case Mips::fixup_MICROMIPS_TLS_DTPREL_LO16: |
| 215 | Type = ELF::R_MICROMIPS_TLS_DTPREL_LO16; |
| 216 | break; |
| 217 | case Mips::fixup_MICROMIPS_TLS_TPREL_HI16: |
| 218 | Type = ELF::R_MICROMIPS_TLS_TPREL_HI16; |
| 219 | break; |
| 220 | case Mips::fixup_MICROMIPS_TLS_TPREL_LO16: |
| 221 | Type = ELF::R_MICROMIPS_TLS_TPREL_LO16; |
| 222 | break; |
Zoran Jovanovic | b9c07f3 | 2014-06-12 12:40:00 +0000 | [diff] [blame] | 223 | case Mips::fixup_MIPS_PC19_S2: |
| 224 | Type = ELF::R_MIPS_PC19_S2; |
| 225 | break; |
Zoran Jovanovic | a5acdcf | 2014-06-13 14:26:47 +0000 | [diff] [blame] | 226 | case Mips::fixup_MIPS_PC18_S3: |
| 227 | Type = ELF::R_MIPS_PC18_S3; |
| 228 | break; |
Zoran Jovanovic | 10e06da | 2014-05-27 12:55:40 +0000 | [diff] [blame] | 229 | case Mips::fixup_MIPS_PC21_S2: |
| 230 | Type = ELF::R_MIPS_PC21_S2; |
| 231 | break; |
| 232 | case Mips::fixup_MIPS_PC26_S2: |
| 233 | Type = ELF::R_MIPS_PC26_S2; |
| 234 | break; |
Zoran Jovanovic | b355e8f | 2014-05-27 14:58:51 +0000 | [diff] [blame] | 235 | case Mips::fixup_MIPS_PCHI16: |
| 236 | Type = ELF::R_MIPS_PCHI16; |
| 237 | break; |
| 238 | case Mips::fixup_MIPS_PCLO16: |
| 239 | Type = ELF::R_MIPS_PCLO16; |
| 240 | break; |
Rafael Espindola | 1dc45d8 | 2011-12-22 03:03:17 +0000 | [diff] [blame] | 241 | } |
Rafael Espindola | 1dc45d8 | 2011-12-22 03:03:17 +0000 | [diff] [blame] | 242 | return Type; |
| 243 | } |
| 244 | |
Petar Jovanovic | 0380d0b | 2015-04-14 13:23:34 +0000 | [diff] [blame] | 245 | // Sort entries by SortOffset in descending order. |
| 246 | // When there are more *HI16 relocs paired with one *LO16 reloc, the 2nd rule |
| 247 | // sorts them in ascending order of R.Offset. |
| 248 | static int cmpRelMips(const MipsRelocationEntry *AP, |
| 249 | const MipsRelocationEntry *BP) { |
| 250 | const MipsRelocationEntry &A = *AP; |
| 251 | const MipsRelocationEntry &B = *BP; |
| 252 | if (A.SortOffset != B.SortOffset) |
| 253 | return B.SortOffset - A.SortOffset; |
| 254 | if (A.R.Offset != B.R.Offset) |
| 255 | return A.R.Offset - B.R.Offset; |
| 256 | if (B.R.Type != A.R.Type) |
| 257 | return B.R.Type - A.R.Type; |
| 258 | //llvm_unreachable("ELFRelocs might be unstable!"); |
| 259 | return 0; |
| 260 | } |
| 261 | |
| 262 | // For the given Reloc.Type, return the matching relocation type, as in the |
| 263 | // table below. |
| 264 | static unsigned getMatchingLoType(const MCAssembler &Asm, |
| 265 | const ELFRelocationEntry &Reloc) { |
| 266 | unsigned Type = Reloc.Type; |
| 267 | if (Type == ELF::R_MIPS_HI16) |
| 268 | return ELF::R_MIPS_LO16; |
| 269 | if (Type == ELF::R_MICROMIPS_HI16) |
| 270 | return ELF::R_MICROMIPS_LO16; |
| 271 | if (Type == ELF::R_MIPS16_HI16) |
| 272 | return ELF::R_MIPS16_LO16; |
| 273 | |
Rafael Espindola | e3b2acf | 2015-05-29 18:47:23 +0000 | [diff] [blame] | 274 | if (MCELF::GetBinding(*Reloc.Symbol) != ELF::STB_LOCAL) |
Petar Jovanovic | 0380d0b | 2015-04-14 13:23:34 +0000 | [diff] [blame] | 275 | return ELF::R_MIPS_NONE; |
| 276 | |
| 277 | if (Type == ELF::R_MIPS_GOT16) |
| 278 | return ELF::R_MIPS_LO16; |
| 279 | if (Type == ELF::R_MICROMIPS_GOT16) |
| 280 | return ELF::R_MICROMIPS_LO16; |
| 281 | if (Type == ELF::R_MIPS16_GOT16) |
| 282 | return ELF::R_MIPS16_LO16; |
| 283 | |
| 284 | return ELF::R_MIPS_NONE; |
| 285 | } |
| 286 | |
| 287 | // Return true if First needs a matching *LO16, its matching *LO16 type equals |
| 288 | // Second's type and both relocations are against the same symbol. |
| 289 | static bool areMatchingHiAndLo(const MCAssembler &Asm, |
| 290 | const ELFRelocationEntry &First, |
| 291 | const ELFRelocationEntry &Second) { |
| 292 | return getMatchingLoType(Asm, First) != ELF::R_MIPS_NONE && |
| 293 | getMatchingLoType(Asm, First) == Second.Type && |
| 294 | First.Symbol && First.Symbol == Second.Symbol; |
| 295 | } |
| 296 | |
| 297 | // Return true if MipsRelocs[Index] is a *LO16 preceded by a matching *HI16. |
| 298 | static bool |
| 299 | isPrecededByMatchingHi(const MCAssembler &Asm, uint32_t Index, |
| 300 | std::vector<MipsRelocationEntry> &MipsRelocs) { |
| 301 | return Index < MipsRelocs.size() - 1 && |
| 302 | areMatchingHiAndLo(Asm, MipsRelocs[Index + 1].R, MipsRelocs[Index].R); |
| 303 | } |
| 304 | |
| 305 | // Return true if MipsRelocs[Index] is a *LO16 not preceded by a matching *HI16 |
| 306 | // and not chosen by a *HI16 as a match. |
| 307 | static bool isFreeLo(const MCAssembler &Asm, uint32_t Index, |
| 308 | std::vector<MipsRelocationEntry> &MipsRelocs) { |
| 309 | return Index < MipsRelocs.size() && !MipsRelocs[Index].HasMatchingHi && |
| 310 | !isPrecededByMatchingHi(Asm, Index, MipsRelocs); |
| 311 | } |
| 312 | |
| 313 | // Lo is chosen as a match for Hi, set their fields accordingly. |
| 314 | // Mips instructions have fixed length of at least two bytes (two for |
| 315 | // micromips/mips16, four for mips32/64), so we can set HI's SortOffset to |
| 316 | // matching LO's Offset minus one to simplify the sorting function. |
| 317 | static void setMatch(MipsRelocationEntry &Hi, MipsRelocationEntry &Lo) { |
| 318 | Lo.HasMatchingHi = true; |
| 319 | Hi.SortOffset = Lo.R.Offset - 1; |
| 320 | } |
| 321 | |
| 322 | // We sort relocation table entries by offset, except for one additional rule |
| 323 | // required by MIPS ABI: every *HI16 relocation must be immediately followed by |
| 324 | // the corresponding *LO16 relocation. We also support a GNU extension that |
| 325 | // allows more *HI16s paired with one *LO16. |
| 326 | // |
| 327 | // *HI16 relocations and their matching *LO16 are: |
| 328 | // |
| 329 | // +---------------------------------------------+-------------------+ |
| 330 | // | *HI16 | matching *LO16 | |
| 331 | // |---------------------------------------------+-------------------| |
| 332 | // | R_MIPS_HI16, local R_MIPS_GOT16 | R_MIPS_LO16 | |
| 333 | // | R_MICROMIPS_HI16, local R_MICROMIPS_GOT16 | R_MICROMIPS_LO16 | |
| 334 | // | R_MIPS16_HI16, local R_MIPS16_GOT16 | R_MIPS16_LO16 | |
| 335 | // +---------------------------------------------+-------------------+ |
| 336 | // |
| 337 | // (local R_*_GOT16 meaning R_*_GOT16 against the local symbol.) |
| 338 | // |
| 339 | // To handle *HI16 and *LO16 relocations, the linker needs a combined addend |
| 340 | // ("AHL") calculated from both *HI16 ("AHI") and *LO16 ("ALO") relocations: |
| 341 | // AHL = (AHI << 16) + (short)ALO; |
| 342 | // |
| 343 | // We are reusing gnu as sorting algorithm so we are emitting the relocation |
| 344 | // table sorted the same way as gnu as would sort it, for easier comparison of |
| 345 | // the generated .o files. |
| 346 | // |
| 347 | // The logic is: |
| 348 | // search the table (starting from the highest offset and going back to zero) |
| 349 | // for all *HI16 relocations that don't have a matching *LO16. |
| 350 | // For every such HI, find a matching LO with highest offset that isn't already |
| 351 | // matched with another HI. If there are no free LOs, match it with the first |
| 352 | // found (starting from lowest offset). |
| 353 | // When there are more HIs matched with one LO, sort them in descending order by |
| 354 | // offset. |
| 355 | // |
| 356 | // In other words, when searching for a matching LO: |
| 357 | // - don't look for a 'better' match for the HIs that are already followed by a |
| 358 | // matching LO; |
| 359 | // - prefer LOs without a pair; |
| 360 | // - prefer LOs with higher offset; |
| 361 | void MipsELFObjectWriter::sortRelocs(const MCAssembler &Asm, |
| 362 | std::vector<ELFRelocationEntry> &Relocs) { |
| 363 | if (Relocs.size() < 2) |
| 364 | return; |
| 365 | |
| 366 | // The default function sorts entries by Offset in descending order. |
| 367 | MCELFObjectTargetWriter::sortRelocs(Asm, Relocs); |
| 368 | |
| 369 | // Init MipsRelocs from Relocs. |
| 370 | std::vector<MipsRelocationEntry> MipsRelocs; |
| 371 | for (unsigned I = 0, E = Relocs.size(); I != E; ++I) |
| 372 | MipsRelocs.push_back(MipsRelocationEntry(Relocs[I])); |
| 373 | |
| 374 | // Find a matching LO for all HIs that need it. |
| 375 | for (int32_t I = 0, E = MipsRelocs.size(); I != E; ++I) { |
| 376 | if (getMatchingLoType(Asm, MipsRelocs[I].R) == ELF::R_MIPS_NONE || |
| 377 | (I > 0 && isPrecededByMatchingHi(Asm, I - 1, MipsRelocs))) |
| 378 | continue; |
| 379 | |
| 380 | int32_t MatchedLoIndex = -1; |
| 381 | |
| 382 | // Search the list in the ascending order of Offset. |
| 383 | for (int32_t J = MipsRelocs.size() - 1, N = -1; J != N; --J) { |
| 384 | // check for a match |
| 385 | if (areMatchingHiAndLo(Asm, MipsRelocs[I].R, MipsRelocs[J].R) && |
| 386 | (MatchedLoIndex == -1 || // first match |
| 387 | // or we already have a match, |
| 388 | // but this one is with higher offset and it's free |
| 389 | (MatchedLoIndex > J && isFreeLo(Asm, J, MipsRelocs)))) |
| 390 | MatchedLoIndex = J; |
| 391 | } |
| 392 | |
| 393 | if (MatchedLoIndex != -1) |
| 394 | // We have a match. |
| 395 | setMatch(MipsRelocs[I], MipsRelocs[MatchedLoIndex]); |
| 396 | } |
| 397 | |
| 398 | // SortOffsets are calculated, call the sorting function. |
| 399 | array_pod_sort(MipsRelocs.begin(), MipsRelocs.end(), cmpRelMips); |
| 400 | |
| 401 | // Copy sorted MipsRelocs back to Relocs. |
| 402 | for (unsigned I = 0, E = MipsRelocs.size(); I != E; ++I) |
| 403 | Relocs[I] = MipsRelocs[I].R; |
| 404 | } |
| 405 | |
Rafael Espindola | ece40ca | 2015-05-29 18:26:09 +0000 | [diff] [blame] | 406 | bool MipsELFObjectWriter::needsRelocateWithSymbol(const MCSymbol &Sym, |
| 407 | unsigned Type) const { |
Mohit K. Bhakkad | e38c32f | 2014-12-09 06:31:07 +0000 | [diff] [blame] | 408 | // FIXME: This is extremely conservative. This really needs to use a |
Rafael Espindola | 5904e12 | 2014-03-29 06:26:49 +0000 | [diff] [blame] | 409 | // whitelist with a clear explanation for why each realocation needs to |
| 410 | // point to the symbol, not to the section. |
| 411 | switch (Type) { |
| 412 | default: |
| 413 | return true; |
| 414 | |
Rafael Espindola | ee1c342 | 2014-03-31 19:00:23 +0000 | [diff] [blame] | 415 | case ELF::R_MIPS_GOT16: |
| 416 | case ELF::R_MIPS16_GOT16: |
| 417 | case ELF::R_MICROMIPS_GOT16: |
| 418 | llvm_unreachable("Should have been handled already"); |
| 419 | |
| 420 | // These relocations might be paired with another relocation. The pairing is |
| 421 | // done by the static linker by matching the symbol. Since we only see one |
| 422 | // relocation at a time, we have to force them to relocate with a symbol to |
| 423 | // avoid ending up with a pair where one points to a section and another |
| 424 | // points to a symbol. |
Rafael Espindola | 5904e12 | 2014-03-29 06:26:49 +0000 | [diff] [blame] | 425 | case ELF::R_MIPS_HI16: |
Rafael Espindola | ee1c342 | 2014-03-31 19:00:23 +0000 | [diff] [blame] | 426 | case ELF::R_MIPS16_HI16: |
| 427 | case ELF::R_MICROMIPS_HI16: |
| 428 | case ELF::R_MIPS_LO16: |
| 429 | case ELF::R_MIPS16_LO16: |
| 430 | case ELF::R_MICROMIPS_LO16: |
| 431 | return true; |
| 432 | |
Rafael Espindola | 5904e12 | 2014-03-29 06:26:49 +0000 | [diff] [blame] | 433 | case ELF::R_MIPS_32: |
Rafael Espindola | e3b2acf | 2015-05-29 18:47:23 +0000 | [diff] [blame] | 434 | if (MCELF::getOther(Sym) & (ELF::STO_MIPS_MICROMIPS >> 2)) |
Zoran Jovanovic | 1064691 | 2014-12-30 22:04:16 +0000 | [diff] [blame] | 435 | return true; |
| 436 | // falltrough |
| 437 | case ELF::R_MIPS_26: |
Rafael Espindola | 5904e12 | 2014-03-29 06:26:49 +0000 | [diff] [blame] | 438 | case ELF::R_MIPS_64: |
| 439 | case ELF::R_MIPS_GPREL16: |
Akira Hatanaka | 5ba593f | 2012-03-28 00:23:33 +0000 | [diff] [blame] | 440 | return false; |
Akira Hatanaka | 5ba593f | 2012-03-28 00:23:33 +0000 | [diff] [blame] | 441 | } |
Akira Hatanaka | 5ba593f | 2012-03-28 00:23:33 +0000 | [diff] [blame] | 442 | } |
| 443 | |
Rafael Espindola | 5560a4c | 2015-04-14 22:14:34 +0000 | [diff] [blame] | 444 | MCObjectWriter *llvm::createMipsELFObjectWriter(raw_pwrite_stream &OS, |
| 445 | uint8_t OSABI, |
Akira Hatanaka | b1f68f9 | 2012-04-02 19:25:22 +0000 | [diff] [blame] | 446 | bool IsLittleEndian, |
| 447 | bool Is64Bit) { |
Simon Atanasyan | c99ce68 | 2015-03-24 12:24:56 +0000 | [diff] [blame] | 448 | MCELFObjectTargetWriter *MOTW = |
| 449 | new MipsELFObjectWriter(Is64Bit, OSABI, Is64Bit, IsLittleEndian); |
Rafael Espindola | 1dc45d8 | 2011-12-22 03:03:17 +0000 | [diff] [blame] | 450 | return createELFObjectWriter(MOTW, OS, IsLittleEndian); |
| 451 | } |