diff --git a/llvm/test/tools/llvm-objcopy/ELF/same-permissions.test b/llvm/test/tools/llvm-objcopy/ELF/same-permissions.test new file mode 100644 --- /dev/null +++ b/llvm/test/tools/llvm-objcopy/ELF/same-permissions.test @@ -0,0 +1,6 @@ +# REQUIRES: system-linux +# RUN: llvm-objcopy %p/Inputs/alloc-symtab.o %t +# RUN: stat --printf=%a %p/Inputs/alloc-symtab.o > %t1 +# RUN: stat --printf=%a %t > %t2 +# RUN: diff %t1 %t2 + diff --git a/llvm/test/tools/llvm-objcopy/ELF/strip-all-and-remove.test b/llvm/test/tools/llvm-objcopy/ELF/strip-all-and-remove.test --- a/llvm/test/tools/llvm-objcopy/ELF/strip-all-and-remove.test +++ b/llvm/test/tools/llvm-objcopy/ELF/strip-all-and-remove.test @@ -1,5 +1,5 @@ # RUN: yaml2obj %s > %t -# RUN: cp %t %t1 +# RUN: cp -p %t %t1 # RUN: llvm-strip --remove-section=.text.bar %t # RUN: llvm-readobj --file-headers --sections --symbols %t | FileCheck %s diff --git a/llvm/tools/llvm-objcopy/Buffer.cpp b/llvm/tools/llvm-objcopy/Buffer.cpp --- a/llvm/tools/llvm-objcopy/Buffer.cpp +++ b/llvm/tools/llvm-objcopy/Buffer.cpp @@ -36,7 +36,7 @@ } Expected> BufferOrErr = - FileOutputBuffer::create(getName(), Size, FileOutputBuffer::F_executable); + FileOutputBuffer::create(getName(), Size); // FileOutputBuffer::create() returns an Error that is just a wrapper around // std::error_code. Wrap it in FileError to include the actual filename. if (!BufferOrErr) diff --git a/llvm/tools/llvm-objcopy/llvm-objcopy.cpp b/llvm/tools/llvm-objcopy/llvm-objcopy.cpp --- a/llvm/tools/llvm-objcopy/llvm-objcopy.cpp +++ b/llvm/tools/llvm-objcopy/llvm-objcopy.cpp @@ -206,9 +206,8 @@ /// format-agnostic modifications, i.e. preserving dates. static Error executeObjcopy(const CopyConfig &Config) { sys::fs::file_status Stat; - if (Config.PreserveDates) - if (auto EC = sys::fs::status(Config.InputFilename, Stat)) - return createFileError(Config.InputFilename, EC); + if (auto EC = sys::fs::status(Config.InputFilename, Stat)) + return createFileError(Config.InputFilename, EC); if (Config.InputFormat == "binary") { auto BufOrErr = MemoryBuffer::getFile(Config.InputFilename); @@ -241,6 +240,10 @@ if (Error E = restoreDateOnFile(Config.SplitDWO, Stat)) return E; } + if (Config.OutputFilename != "-") + if (std::error_code E = + sys::fs::setPermissions(Config.OutputFilename, Stat.permissions())) + return errorCodeToError(E); return Error::success(); }