1 //===- ELFAttributeValue.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 
10 #include <mcld/Target/ELFAttributeValue.h>
11 
12 #include <llvm/Support/ErrorHandling.h>
13 
14 #include <mcld/Support/LEB128.h>
15 
16 using namespace mcld;
17 
getSize() const18 size_t ELFAttributeValue::getSize() const
19 {
20   size_t size = 0;
21 
22   if (isIntValue())
23     size += leb128::size<uint32_t>(m_IntValue);
24 
25   if (isStringValue())
26     size += m_StringValue.length() + 1 /* for NULL-terminator */;
27 
28   if (size <= 0)
29     // unknown type of the value
30     llvm::report_fatal_error("Unknown type of attribtue value!");
31 
32   return size;
33 }
34 
isDefaultValue() const35 bool ELFAttributeValue::isDefaultValue() const
36 {
37   if (isUninitialized()) {
38     // Uninitialized attribute means default value
39     return true;
40   } else {
41     if (isIntValue() && (m_IntValue != 0))
42       return false;
43     if (isStringValue() && !m_StringValue.empty())
44       return false;
45 
46     // The value hold in the storage is the "default value by default" (i.e., 0
47     // for int type, empty string for string type), but we need to check whether
48     // the "default value" is defined (that is, hasNoDefault() = false.)
49     return !hasNoDefault();
50   }
51   // unreachable
52 }
53 
equals(const ELFAttributeValue & pValue) const54 bool ELFAttributeValue::equals(const ELFAttributeValue& pValue) const
55 {
56   if ((pValue.type() != m_Type) || isUninitialized())
57     return false;
58 
59   if (isIntValue() && (m_IntValue != pValue.getIntValue()))
60     return false;
61 
62   if (isStringValue() && (m_StringValue != pValue.getStringValue()))
63     return false;
64 
65   return true;
66 }
67