aboutsummaryrefslogtreecommitdiff
path: root/lib/LD/MergedStringTable.cpp
blob: 105fcb26545cbbe663d23d91690607b5fda37e11 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
//===- MergedStringTable.cpp ----------------------------------------------===//
//
//                     The MCLinker Project
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include <mcld/LD/MergedStringTable.h>

using namespace mcld;

MergedStringTable::StringMapEntryTy&
MergedStringTable::getOrCreateString(llvm::StringRef pString)
{
  return m_StringMap.GetOrCreateValue(pString);
}

uint64_t MergedStringTable::finalizeOffset()
{
  // trverse the string table and set the offset
  string_map_iterator it, end = m_StringMap.end();
  size_t offset = 0;
  for (it = m_StringMap.begin(); it != end; ++it) {
    it->setValue(offset);
    offset += it->getKey().size() + 1;
  }
  return offset;
}

void MergedStringTable::emit(MemoryRegion& pRegion)
{
  char* ptr = (char*)pRegion.begin();
  string_map_iterator it, end = m_StringMap.end();
  for (it = m_StringMap.begin(); it != end; ++it) {
    strcpy(ptr, it->getKey().data());
    ptr += it->getKey().size() + 1;
  }
}

size_t MergedStringTable::getOutputOffset(MergedStringTable::StringMapEntryTy& pEntry)
{
  return pEntry.getValue();
}

size_t MergedStringTable::getOutputOffset(llvm::StringRef pStr)
{
  assert(m_StringMap.find(pStr) != m_StringMap.end());
  return m_StringMap[pStr];
}