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