17
17
#include " llvm/Support/FileOutputBuffer.h"
18
18
#include " llvm/Support/MathExtras.h"
19
19
#include " llvm/Support/MemoryBuffer.h"
20
- #include " llvm/Support/Process.h"
21
20
22
21
using llvm::MemoryBufferRef;
23
22
using llvm::object::ELFObjectFile;
@@ -664,25 +663,8 @@ buildStub(const ELFObjectFile<ELFT> &ElfObj) {
664
663
// / @param FilePath File path for writing the ELF binary.
665
664
// / @param Stub Source ELFStub to generate a binary ELF stub from.
666
665
template <class ELFT >
667
- static Error writeELFBinaryToFile (StringRef FilePath, const ELFStub &Stub,
668
- bool WriteIfChanged) {
666
+ static Error writeELFBinaryToFile (StringRef FilePath, const ELFStub &Stub) {
669
667
ELFStubBuilder<ELFT> Builder{Stub};
670
- // Write Stub to memory first.
671
- std::vector<uint8_t > Buf (Builder.getSize ());
672
- Builder.write (Buf.data ());
673
-
674
- if (WriteIfChanged) {
675
- if (ErrorOr<std::unique_ptr<MemoryBuffer>> BufOrError =
676
- MemoryBuffer::getFile (FilePath)) {
677
- // Compare Stub output with existing Stub file.
678
- // If Stub file unchanged, abort updating.
679
- if ((*BufOrError)->getBufferSize () == Builder.getSize () &&
680
- !memcmp ((*BufOrError)->getBufferStart (), Buf.data (),
681
- Builder.getSize ()))
682
- return Error::success ();
683
- }
684
- }
685
-
686
668
Expected<std::unique_ptr<FileOutputBuffer>> BufOrError =
687
669
FileOutputBuffer::create (FilePath, Builder.getSize ());
688
670
if (!BufOrError)
@@ -692,10 +674,13 @@ static Error writeELFBinaryToFile(StringRef FilePath, const ELFStub &Stub,
692
674
" ` for writing" );
693
675
694
676
// Write binary to file.
695
- std::unique_ptr<FileOutputBuffer> FileBuf = std::move (*BufOrError);
696
- memcpy (FileBuf ->getBufferStart (), Buf. data (), Buf. size ());
677
+ std::unique_ptr<FileOutputBuffer> Buf = std::move (*BufOrError);
678
+ Builder. write (Buf ->getBufferStart ());
697
679
698
- return FileBuf->commit ();
680
+ if (Error E = Buf->commit ())
681
+ return E;
682
+
683
+ return Error::success ();
699
684
}
700
685
701
686
Expected<std::unique_ptr<ELFStub>> readELFFile (MemoryBufferRef Buf) {
@@ -720,15 +705,15 @@ Expected<std::unique_ptr<ELFStub>> readELFFile(MemoryBufferRef Buf) {
720
705
// This function wraps the ELFT writeELFBinaryToFile() so writeBinaryStub()
721
706
// can be called without having to use ELFType templates directly.
722
707
Error writeBinaryStub (StringRef FilePath, const ELFStub &Stub,
723
- ELFTarget OutputFormat, bool WriteIfChanged ) {
708
+ ELFTarget OutputFormat) {
724
709
if (OutputFormat == ELFTarget::ELF32LE)
725
- return writeELFBinaryToFile<ELF32LE>(FilePath, Stub, WriteIfChanged );
710
+ return writeELFBinaryToFile<ELF32LE>(FilePath, Stub);
726
711
if (OutputFormat == ELFTarget::ELF32BE)
727
- return writeELFBinaryToFile<ELF32BE>(FilePath, Stub, WriteIfChanged );
712
+ return writeELFBinaryToFile<ELF32BE>(FilePath, Stub);
728
713
if (OutputFormat == ELFTarget::ELF64LE)
729
- return writeELFBinaryToFile<ELF64LE>(FilePath, Stub, WriteIfChanged );
714
+ return writeELFBinaryToFile<ELF64LE>(FilePath, Stub);
730
715
if (OutputFormat == ELFTarget::ELF64BE)
731
- return writeELFBinaryToFile<ELF64BE>(FilePath, Stub, WriteIfChanged );
716
+ return writeELFBinaryToFile<ELF64BE>(FilePath, Stub);
732
717
llvm_unreachable (" invalid binary output target" );
733
718
}
734
719
0 commit comments