Add BinaryRef binary_size() method.
authorSean Silva <silvas@purdue.edu>
Wed, 5 Jun 2013 23:32:27 +0000 (23:32 +0000)
committerSean Silva <silvas@purdue.edu>
Wed, 5 Jun 2013 23:32:27 +0000 (23:32 +0000)
This avoids making assumptions about the data representation.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@183349 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/Object/YAML.h
tools/yaml2obj/yaml2coff.cpp

index e6f1da1e1d4253917c57f11847a9c35d490069ad..92811b04eb3371b535db87a27f9d4747663e2757 100644 (file)
@@ -43,6 +43,13 @@ public:
     assert(isBinary);
     return Data;
   }
+  /// \brief The number of bytes that are represented by this BinaryRef.
+  /// This is the number of bytes that writeAsBinary() will write.
+  ArrayRef<uint8_t>::size_type binary_size() const {
+    if (!isBinary)
+      return Data.size() / 2;
+    return Data.size();
+  }
   bool operator==(const BinaryRef &Ref) {
     // Special case for default constructed BinaryRef.
     if (Ref.Data.empty() && Data.empty())
index 5b758471bdcea15b413692ce8352b66f4bf1f7e5..d800b90791cffdbaaf2ff87f37fe9512dc892759 100644 (file)
@@ -129,9 +129,8 @@ static bool layoutCOFF(COFFParser &CP) {
   for (std::vector<COFFYAML::Section>::iterator i = CP.Obj.Sections.begin(),
                                                 e = CP.Obj.Sections.end();
                                                 i != e; ++i) {
-    StringRef SecData = i->SectionData.getHex();
-    if (!SecData.empty()) {
-      i->Header.SizeOfRawData = SecData.size()/2;
+    if (i->SectionData.binary_size() > 0) {
+      i->Header.SizeOfRawData = i->SectionData.binary_size();
       i->Header.PointerToRawData = CurrentSectionDataOffset;
       CurrentSectionDataOffset += i->Header.SizeOfRawData;
       if (!i->Relocations.empty()) {
@@ -154,7 +153,7 @@ static bool layoutCOFF(COFFParser &CP) {
   for (std::vector<COFFYAML::Symbol>::iterator i = CP.Obj.Symbols.begin(),
                                                e = CP.Obj.Symbols.end();
                                                i != e; ++i) {
-    unsigned AuxBytes = i->AuxiliaryData.getHex().size() / 2;
+    unsigned AuxBytes = i->AuxiliaryData.binary_size();
     if (AuxBytes % COFF::SymbolSize != 0) {
       errs() << "AuxiliaryData size not a multiple of symbol size!\n";
       return false;