Split getFdeEncoding.
authorRui Ueyama <ruiu@google.com>
Sat, 19 Nov 2016 18:44:09 +0000 (18:44 +0000)
committerRui Ueyama <ruiu@google.com>
Sat, 19 Nov 2016 18:44:09 +0000 (18:44 +0000)
llvm-svn: 287452

lld/ELF/EhFrame.cpp

index c1e7b2d..29d4cb4 100644 (file)
@@ -18,6 +18,7 @@
 
 #include "EhFrame.h"
 #include "Error.h"
+#include "Strings.h"
 
 #include "llvm/Object/ELF.h"
 #include "llvm/Support/Dwarf.h"
@@ -60,6 +61,16 @@ static uint8_t readByte(ArrayRef<uint8_t> &D) {
   return B;
 }
 
+// Read a null-terminated string.
+static StringRef readString(ArrayRef<uint8_t> &D) {
+  const uint8_t *End = std::find(D.begin(), D.end(), '\0');
+  if (End == D.end())
+    fatal("corrupted CIE");
+  StringRef S = toStringRef(D.slice(0, End - D.begin()));
+  D = D.slice(S.size() + 1);
+  return S;
+}
+
 // Skip an integer encoded in the LEB128 format.
 // Actual number is not of interest because only the runtime needs it.
 // But we need to be at least able to skip it so that we can read
@@ -107,20 +118,14 @@ template <class ELFT> uint8_t getFdeEncoding(ArrayRef<uint8_t> D) {
     fatal("CIE too small");
   D = D.slice(8);
 
-  uint8_t Version = readByte(D);
+  int Version = readByte(D);
   if (Version != 1 && Version != 3)
-    fatal("FDE version 1 or 3 expected, but got " + Twine((unsigned)Version));
+    fatal("FDE version 1 or 3 expected, but got " + Twine(Version));
 
-  const unsigned char *AugEnd = std::find(D.begin(), D.end(), '\0');
-  if (AugEnd == D.end())
-    fatal("corrupted CIE");
-  StringRef Aug(reinterpret_cast<const char *>(D.begin()), AugEnd - D.begin());
-  D = D.slice(Aug.size() + 1);
+  StringRef Aug = readString(D);
 
-  // Skip code alignment factor.
+  // Skip code and data alignment factors.
   skipLeb128(D);
-
-  // Skip data alignment factor.
   skipLeb128(D);
 
   // Skip the return address register. In CIE version 1 this is a single