[llvm] [ELF][Objcopy] Dont corrupt symbol table when `--update-section` is called for ELF files (PR #170462)

Dmitry Nechitaev via llvm-commits llvm-commits at lists.llvm.org
Sat Dec 6 03:56:00 PST 2025


================
@@ -2171,7 +2171,14 @@ Error Object::updateSectionData(SecPtr &Sec, ArrayRef<uint8_t> Data) {
                              Data.size(), Sec->Name.c_str(), Sec->Size);
 
   if (!Sec->ParentSegment) {
-    Sec = std::make_unique<OwnedDataSection>(*Sec, Data);
+    // Be careful: the "Sec" refers to an item in a std::vector,
+    // so any changes to the std::vector could invalidate this reference.
+    // To avoid possible memory-related issues, save raw pointers.
+    auto Replaced = Sec.get();
+    auto Modified = &addSection<OwnedDataSection>(*Sec, Data);
----------------
Nechda wrote:

You are right, the `addSection` function does add one more section with the same name. However, the function `replaceSections`, which is called two lines later, removes the old section and replaces its pointer with the new one.

https://github.com/llvm/llvm-project/blob/830ef4e4e2d0f181ed1cfacbc3daacb2555ff9e4/llvm/lib/ObjCopy/ELF/ELFObject.cpp#L2273-L2282

https://github.com/llvm/llvm-project/pull/170462


More information about the llvm-commits mailing list