Stephen Hines | f33f6de | 2014-02-14 18:00:16 -0800 | [diff] [blame] | 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 | |
Stephen Hines | 37b74a3 | 2014-11-26 18:48:20 -0800 | [diff] [blame] | 10 | #include "mcld/Target/ELFAttributeValue.h" |
Stephen Hines | f33f6de | 2014-02-14 18:00:16 -0800 | [diff] [blame] | 11 | |
| 12 | #include <llvm/Support/ErrorHandling.h> |
| 13 | |
Stephen Hines | 37b74a3 | 2014-11-26 18:48:20 -0800 | [diff] [blame] | 14 | #include "mcld/Support/LEB128.h" |
Stephen Hines | f33f6de | 2014-02-14 18:00:16 -0800 | [diff] [blame] | 15 | |
Stephen Hines | 37b74a3 | 2014-11-26 18:48:20 -0800 | [diff] [blame] | 16 | namespace mcld { |
Stephen Hines | f33f6de | 2014-02-14 18:00:16 -0800 | [diff] [blame] | 17 | |
Stephen Hines | 37b74a3 | 2014-11-26 18:48:20 -0800 | [diff] [blame] | 18 | size_t ELFAttributeValue::getSize() const { |
Stephen Hines | f33f6de | 2014-02-14 18:00:16 -0800 | [diff] [blame] | 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 | |
Stephen Hines | 37b74a3 | 2014-11-26 18:48:20 -0800 | [diff] [blame] | 34 | bool ELFAttributeValue::isDefaultValue() const { |
Stephen Hines | f33f6de | 2014-02-14 18:00:16 -0800 | [diff] [blame] | 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 | |
Stephen Hines | 37b74a3 | 2014-11-26 18:48:20 -0800 | [diff] [blame] | 52 | bool ELFAttributeValue::equals(const ELFAttributeValue& pValue) const { |
Stephen Hines | f33f6de | 2014-02-14 18:00:16 -0800 | [diff] [blame] | 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 | } |
Stephen Hines | 37b74a3 | 2014-11-26 18:48:20 -0800 | [diff] [blame] | 64 | |
| 65 | } // namespace mcld |