1 //===- ELFSegment.cpp -----------------------------------------------------===//
2 //
3 //                     The MCLinker Project
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 #include "mcld/LD/ELFSegment.h"
10 
11 #include "mcld/Config/Config.h"
12 #include "mcld/LD/LDSection.h"
13 #include "mcld/Support/GCFactory.h"
14 
15 #include <llvm/Support/ManagedStatic.h>
16 
17 #include <cassert>
18 
19 namespace mcld {
20 
21 typedef GCFactory<ELFSegment, MCLD_SEGMENTS_PER_OUTPUT> ELFSegmentFactory;
22 static llvm::ManagedStatic<ELFSegmentFactory> g_ELFSegmentFactory;
23 
24 //===----------------------------------------------------------------------===//
25 // ELFSegment
26 //===----------------------------------------------------------------------===//
ELFSegment()27 ELFSegment::ELFSegment()
28     : m_Type(llvm::ELF::PT_NULL),
29       m_Flag(llvm::ELF::PF_R),
30       m_Offset(0x0),
31       m_Vaddr(0x0),
32       m_Paddr(0x0),
33       m_Filesz(0x0),
34       m_Memsz(0x0),
35       m_Align(0x0),
36       m_MaxSectionAlign(0x0) {
37 }
38 
ELFSegment(uint32_t pType,uint32_t pFlag)39 ELFSegment::ELFSegment(uint32_t pType, uint32_t pFlag)
40     : m_Type(pType),
41       m_Flag(pFlag),
42       m_Offset(0x0),
43       m_Vaddr(0x0),
44       m_Paddr(0x0),
45       m_Filesz(0x0),
46       m_Memsz(0x0),
47       m_Align(0x0),
48       m_MaxSectionAlign(0x0) {
49 }
50 
~ELFSegment()51 ELFSegment::~ELFSegment() {
52 }
53 
isLoadSegment() const54 bool ELFSegment::isLoadSegment() const {
55   return type() == llvm::ELF::PT_LOAD;
56 }
57 
isDataSegment() const58 bool ELFSegment::isDataSegment() const {
59   return (type() == llvm::ELF::PT_LOAD) && ((flag() & llvm::ELF::PF_W) != 0x0);
60 }
61 
isBssSegment() const62 bool ELFSegment::isBssSegment() const {
63   if (!isDataSegment())
64     return false;
65   for (const_iterator it = begin(), ie = end(); it != ie; ++it) {
66     if ((*it)->kind() != LDFileFormat::BSS)
67       return false;
68   }
69   return true;
70 }
71 
insert(ELFSegment::iterator pPos,LDSection * pSection)72 ELFSegment::iterator ELFSegment::insert(ELFSegment::iterator pPos,
73                                         LDSection* pSection) {
74   return m_SectionList.insert(pPos, pSection);
75 }
76 
append(LDSection * pSection)77 void ELFSegment::append(LDSection* pSection) {
78   assert(pSection != NULL);
79   if (pSection->align() > m_MaxSectionAlign)
80     m_MaxSectionAlign = pSection->align();
81   m_SectionList.push_back(pSection);
82 }
83 
Create(uint32_t pType,uint32_t pFlag)84 ELFSegment* ELFSegment::Create(uint32_t pType, uint32_t pFlag) {
85   ELFSegment* seg = g_ELFSegmentFactory->allocate();
86   new (seg) ELFSegment(pType, pFlag);
87   return seg;
88 }
89 
Destroy(ELFSegment * & pSegment)90 void ELFSegment::Destroy(ELFSegment*& pSegment) {
91   g_ELFSegmentFactory->destroy(pSegment);
92   g_ELFSegmentFactory->deallocate(pSegment);
93   pSegment = NULL;
94 }
95 
Clear()96 void ELFSegment::Clear() {
97   g_ELFSegmentFactory->clear();
98 }
99 
100 }  // namespace mcld
101