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#include <mcld/LD/LDSection.h>
11#include <mcld/Support/GCFactory.h>
12#include <mcld/Config/Config.h>
13#include <llvm/Support/ManagedStatic.h>
14#include <cassert>
15
16using namespace mcld;
17
18typedef GCFactory<ELFSegment, MCLD_SEGMENTS_PER_OUTPUT> ELFSegmentFactory;
19static llvm::ManagedStatic<ELFSegmentFactory> g_ELFSegmentFactory;
20
21//===----------------------------------------------------------------------===//
22// ELFSegment
23//===----------------------------------------------------------------------===//
24ELFSegment::ELFSegment()
25  : m_Type(llvm::ELF::PT_NULL),
26    m_Flag(llvm::ELF::PF_R),
27    m_Offset(0x0),
28    m_Vaddr(0x0),
29    m_Paddr(0x0),
30    m_Filesz(0x0),
31    m_Memsz(0x0),
32    m_Align(0x0),
33    m_MaxSectionAlign(0x0)
34{
35}
36
37ELFSegment::ELFSegment(uint32_t pType, uint32_t pFlag)
38  : m_Type(pType),
39    m_Flag(pFlag),
40    m_Offset(0x0),
41    m_Vaddr(0x0),
42    m_Paddr(0x0),
43    m_Filesz(0x0),
44    m_Memsz(0x0),
45    m_Align(0x0),
46    m_MaxSectionAlign(0x0)
47{
48}
49
50ELFSegment::~ELFSegment()
51{
52}
53
54bool ELFSegment::isLoadSegment() const
55{
56  return type() == llvm::ELF::PT_LOAD;
57}
58
59bool ELFSegment::isDataSegment() const
60{
61  return (type() == llvm::ELF::PT_LOAD) && ((flag() & llvm::ELF::PF_W) != 0x0);
62}
63
64bool ELFSegment::isBssSegment() const
65{
66  if (!isDataSegment())
67    return false;
68  for (const_iterator it = begin(), ie = end(); it != ie; ++it) {
69    if ((*it)->kind() != LDFileFormat::BSS)
70      return false;
71  }
72  return true;
73}
74
75ELFSegment::iterator ELFSegment::insert(ELFSegment::iterator pPos,
76                                        LDSection* pSection)
77{
78  return m_SectionList.insert(pPos, pSection);
79}
80
81void ELFSegment::append(LDSection* pSection)
82{
83  assert(NULL != pSection);
84  if (pSection->align() > m_MaxSectionAlign)
85    m_MaxSectionAlign = pSection->align();
86  m_SectionList.push_back(pSection);
87}
88
89ELFSegment* ELFSegment::Create(uint32_t pType, uint32_t pFlag)
90{
91  ELFSegment* seg = g_ELFSegmentFactory->allocate();
92  new (seg) ELFSegment(pType, pFlag);
93  return seg;
94}
95
96void ELFSegment::Destroy(ELFSegment*& pSegment)
97{
98  g_ELFSegmentFactory->destroy(pSegment);
99  g_ELFSegmentFactory->deallocate(pSegment);
100  pSegment = NULL;
101}
102
103void ELFSegment::Clear()
104{
105  g_ELFSegmentFactory->clear();
106}
107