blob: fff6c7df6656f7c2ea6dc2435e52ddc97855ef61 [file] [log] [blame]
Stephen Hinesf33f6de2014-02-14 18:00:16 -08001//===- 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
Stephen Hines37b74a32014-11-26 18:48:20 -080010#include "mcld/Target/ELFAttributeValue.h"
Stephen Hinesf33f6de2014-02-14 18:00:16 -080011
12#include <llvm/Support/ErrorHandling.h>
13
Stephen Hines37b74a32014-11-26 18:48:20 -080014#include "mcld/Support/LEB128.h"
Stephen Hinesf33f6de2014-02-14 18:00:16 -080015
Stephen Hines37b74a32014-11-26 18:48:20 -080016namespace mcld {
Stephen Hinesf33f6de2014-02-14 18:00:16 -080017
Stephen Hines37b74a32014-11-26 18:48:20 -080018size_t ELFAttributeValue::getSize() const {
Stephen Hinesf33f6de2014-02-14 18:00:16 -080019 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
Stephen Hines37b74a32014-11-26 18:48:20 -080034bool ELFAttributeValue::isDefaultValue() const {
Stephen Hinesf33f6de2014-02-14 18:00:16 -080035 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
Stephen Hines37b74a32014-11-26 18:48:20 -080052bool ELFAttributeValue::equals(const ELFAttributeValue& pValue) const {
Stephen Hinesf33f6de2014-02-14 18:00:16 -080053 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}
Stephen Hines37b74a32014-11-26 18:48:20 -080064
65} // namespace mcld