The fragment implies the section, don't store both.
[oota-llvm.git] / lib / MC / MCELF.cpp
1 //===- lib/MC/MCELF.cpp - MC ELF ------------------------------------------===//
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 // This file implements ELF object file writer information.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "llvm/MC/MCELF.h"
15 #include "llvm/MC/MCAssembler.h"
16 #include "llvm/MC/MCSymbol.h"
17 #include "llvm/MC/MCELFSymbolFlags.h"
18 #include "llvm/MC/MCFixupKindInfo.h"
19 #include "llvm/Support/ELF.h"
20
21 namespace llvm {
22
23 void MCELF::SetBinding(const MCSymbol &Sym, unsigned Binding) {
24   assert(Binding == ELF::STB_LOCAL || Binding == ELF::STB_GLOBAL ||
25          Binding == ELF::STB_WEAK || Binding == ELF::STB_GNU_UNIQUE);
26   uint32_t OtherFlags = Sym.getFlags() & ~(0xf << ELF_STB_Shift);
27   Sym.setFlags(OtherFlags | (Binding << ELF_STB_Shift));
28 }
29
30 unsigned MCELF::GetBinding(const MCSymbol &Sym) {
31   uint32_t Binding = (Sym.getFlags() & (0xf << ELF_STB_Shift)) >> ELF_STB_Shift;
32   assert(Binding == ELF::STB_LOCAL || Binding == ELF::STB_GLOBAL ||
33          Binding == ELF::STB_WEAK || Binding == ELF::STB_GNU_UNIQUE);
34   return Binding;
35 }
36
37 void MCELF::SetType(const MCSymbol &Sym, unsigned Type) {
38   assert(Type == ELF::STT_NOTYPE || Type == ELF::STT_OBJECT ||
39          Type == ELF::STT_FUNC || Type == ELF::STT_SECTION ||
40          Type == ELF::STT_COMMON || Type == ELF::STT_TLS ||
41          Type == ELF::STT_GNU_IFUNC);
42
43   uint32_t OtherFlags = Sym.getFlags() & ~(0xf << ELF_STT_Shift);
44   Sym.setFlags(OtherFlags | (Type << ELF_STT_Shift));
45 }
46
47 unsigned MCELF::GetType(const MCSymbol &Sym) {
48   uint32_t Type = (Sym.getFlags() & (0xf << ELF_STT_Shift)) >> ELF_STT_Shift;
49   assert(Type == ELF::STT_NOTYPE || Type == ELF::STT_OBJECT ||
50          Type == ELF::STT_FUNC || Type == ELF::STT_SECTION ||
51          Type == ELF::STT_COMMON || Type == ELF::STT_TLS || Type == ELF::STT_GNU_IFUNC);
52   return Type;
53 }
54
55 // Visibility is stored in the first two bits of st_other
56 // st_other values are stored in the second byte of get/setFlags
57 void MCELF::SetVisibility(MCSymbol &Sym, unsigned Visibility) {
58   assert(Visibility == ELF::STV_DEFAULT || Visibility == ELF::STV_INTERNAL ||
59          Visibility == ELF::STV_HIDDEN || Visibility == ELF::STV_PROTECTED);
60
61   uint32_t OtherFlags = Sym.getFlags() & ~(0x3 << ELF_STV_Shift);
62   Sym.setFlags(OtherFlags | (Visibility << ELF_STV_Shift));
63 }
64
65 unsigned MCELF::GetVisibility(const MCSymbol &Sym) {
66   unsigned Visibility =
67       (Sym.getFlags() & (0x3 << ELF_STV_Shift)) >> ELF_STV_Shift;
68   assert(Visibility == ELF::STV_DEFAULT || Visibility == ELF::STV_INTERNAL ||
69          Visibility == ELF::STV_HIDDEN || Visibility == ELF::STV_PROTECTED);
70   return Visibility;
71 }
72
73 // Other is stored in the last six bits of st_other
74 // st_other values are stored in the second byte of get/setFlags
75 void MCELF::setOther(MCSymbol &Sym, unsigned Other) {
76   uint32_t OtherFlags = Sym.getFlags() & ~(0x3f << ELF_STO_Shift);
77   Sym.setFlags(OtherFlags | (Other << ELF_STO_Shift));
78 }
79
80 unsigned MCELF::getOther(const MCSymbol &Sym) {
81   unsigned Other = (Sym.getFlags() & (0x3f << ELF_STO_Shift)) >> ELF_STO_Shift;
82   return Other;
83 }
84
85 }