[Object] Fix reading objects created with -fembed-bitcode-marker

Currently, this fails with many tools, e.g.

$ clang -fembed-bitcode-marker -c -o test.o test.c
$ nm test.o
nm: test.o The file was not recognized as a valid object file

-fembed-bitcode-marker creates a LLVM,bitcode section consisting of a single
byte. When reading the object file, IRObjectFile::findBitcodeInObject succeeds,
causing SymbolicFile::createSymbolicFile to try to read the "bitcode" rather
than using the outer Mach-O data - when then fails.

Fix this by making findBitcodeInObject return an error if the section size <= 1.

Patched by: Nicholas Allegra

Differential Revision: https://reviews.llvm.org/D44373

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@356718 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/Object/IRObjectFile.cpp b/lib/Object/IRObjectFile.cpp
index 5001ac3..d157908 100644
--- a/lib/Object/IRObjectFile.cpp
+++ b/lib/Object/IRObjectFile.cpp
@@ -78,6 +78,8 @@
       StringRef SecContents;
       if (std::error_code EC = Sec.getContents(SecContents))
         return errorCodeToError(EC);
+      if (SecContents.size() <= 1)
+        return errorCodeToError(object_error::bitcode_section_not_found);
       return MemoryBufferRef(SecContents, Obj.getFileName());
     }
   }
diff --git a/test/Object/Inputs/macho-bitcode-marker-x86_64.o b/test/Object/Inputs/macho-bitcode-marker-x86_64.o
new file mode 100644
index 0000000..5b24816
--- /dev/null
+++ b/test/Object/Inputs/macho-bitcode-marker-x86_64.o
Binary files differ
diff --git a/test/Object/Inputs/macho-bitcode-x86_64.o b/test/Object/Inputs/macho-bitcode-x86_64.o
new file mode 100644
index 0000000..eeaec04
--- /dev/null
+++ b/test/Object/Inputs/macho-bitcode-x86_64.o
Binary files differ
diff --git a/test/Object/nm-bitcode.test b/test/Object/nm-bitcode.test
new file mode 100644
index 0000000..f79de9b
--- /dev/null
+++ b/test/Object/nm-bitcode.test
@@ -0,0 +1,12 @@
+# Inputs generated with:
+# echo 'int hello() { return 5; }' > test.c
+# clang -O -fembed-bitcode -c -o macho-bitcode-x86_64.o test.c
+# clang -O -fembed-bitcode-marker -c -o macho-bitcode-marker-x86_64.o test.c
+
+RUN: llvm-nm -a %p/Inputs/macho-bitcode-x86_64.o \
+RUN:         | FileCheck %s
+
+RUN: llvm-nm -a %p/Inputs/macho-bitcode-marker-x86_64.o \
+RUN:         | FileCheck %s
+
+CHECK: T _hello