1//===-- MCELFObjectTargetWriter.cpp - ELF Target Writer Subclass ----------===//
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
10#include "llvm/ADT/STLExtras.h"
11#include "llvm/MC/MCELFObjectWriter.h"
12#include "llvm/MC/MCExpr.h"
13#include "llvm/MC/MCValue.h"
14
15using namespace llvm;
16
17MCELFObjectTargetWriter::MCELFObjectTargetWriter(bool Is64Bit_,
18                                                 uint8_t OSABI_,
19                                                 uint16_t EMachine_,
20                                                 bool HasRelocationAddend_,
21                                                 bool IsN64_)
22  : OSABI(OSABI_), EMachine(EMachine_),
23    HasRelocationAddend(HasRelocationAddend_), Is64Bit(Is64Bit_),
24    IsN64(IsN64_){
25}
26
27const MCSymbol *MCELFObjectTargetWriter::ExplicitRelSym(const MCAssembler &Asm,
28                                                        const MCValue &Target,
29                                                        const MCFragment &F,
30                                                        const MCFixup &Fixup,
31                                                        bool IsPCRel) const {
32  return NULL;
33}
34
35const MCSymbol *MCELFObjectTargetWriter::undefinedExplicitRelSym(const MCValue &Target,
36                                                                 const MCFixup &Fixup,
37                                                                 bool IsPCRel) const {
38  const MCSymbol &Symbol = Target.getSymA()->getSymbol();
39  return &Symbol.AliasedSymbol();
40}
41
42// ELF doesn't require relocations to be in any order. We sort by the r_offset,
43// just to match gnu as for easier comparison. The use type and index is an
44// arbitrary way of making the sort deterministic.
45static int cmpRel(const ELFRelocationEntry *AP, const ELFRelocationEntry *BP) {
46  const ELFRelocationEntry &A = *AP;
47  const ELFRelocationEntry &B = *BP;
48  if (A.r_offset != B.r_offset)
49    return B.r_offset - A.r_offset;
50  if (B.Type != A.Type)
51    return A.Type - B.Type;
52  if (B.Index != A.Index)
53    return B.Index - A.Index;
54  llvm_unreachable("ELFRelocs might be unstable!");
55}
56
57void
58MCELFObjectTargetWriter::sortRelocs(const MCAssembler &Asm,
59                                    std::vector<ELFRelocationEntry> &Relocs) {
60  array_pod_sort(Relocs.begin(), Relocs.end(), cmpRel);
61}
62