Coder Social home page Coder Social logo

Comments (5)

mitchblank avatar mitchblank commented on June 1, 2024 1

Wow, good job finding this old bug. Hope the info in it helped.

I can't be of much assistance to verifying your fix since I filed this in reference to a problem I hit at my day job... which I left 4 years ago. I don't have access to the codebase where I was seeing this problem occur.

from patchelf.

mitchblank avatar mitchblank commented on June 1, 2024

Here is an example of what I'm talking about on a trivial "Hello, World" executable. Before:

$ objdump -x -j .dynamic a.out | tail

Sections:
Idx Name          Size      VMA               LMA               File off  Algn
 20 .dynamic      000001d0  0000000000600e28  0000000000600e28  00000e28  2**3
                  CONTENTS, ALLOC, LOAD, DATA
SYMBOL TABLE:
0000000000600e28 l    d  .dynamic    0000000000000000              .dynamic
0000000000600e28 l     O .dynamic    0000000000000000              _DYNAMIC

So this shows that the .dynamic section is at file offset 0xe28 and will end up mapped at 0x600e28. In the symbol table there is both a debugging symbol called .dynamic and also an object symbol called _DYNAMIC that both point to this section.

Then if I run patchelf on this binary, we'll see this change:

$ patchelf --set-rpath '$ORIGIN/../lib' a.out

$ objdump -x -j .dynamic a.out | tail

Sections:
Idx Name          Size      VMA               LMA               File off  Algn
  0 .dynamic      000001e0  00000000003ff270  00000000003ff270  00000270  2**3
                  CONTENTS, ALLOC, LOAD, DATA
SYMBOL TABLE:
00000000003ff270 l    d  .dynamic   0000000000000000              .dynamic
0000000000600e28 l     O .dynamic   0000000000000000              _DYNAMIC

So the section has now been relocated, both in the file and in virtual address space. The debugging symbol .dynamic is also updated. However the object symbol _DYNAMIC still has its old value and isn't pointing into this ELF table anymore.

from patchelf.

mitchblank avatar mitchblank commented on June 1, 2024

I suspect this behavior is related to this "FIXME" comment in ElfFile<ElfFileParamNames>::rewriteHeaders:

                /* Rewrite st_value.  FIXME: we should do this for all
                   types, but most don't actually change. */
                if (ELF32_ST_TYPE(rdi(sym->st_info)) == STT_SECTION)
                    wri(sym->st_value, rdi(shdrs[newIndex].sh_addr));

Presumably _DYNAMIC will be STT_OBJECT instead...

from patchelf.

mitchblank avatar mitchblank commented on June 1, 2024

This fix would probably look something a little like:

                /* Rewrite st_value. */
                if (ELF32_ST_TYPE(rdi(sym->st_info)) == STT_SECTION) {
                    wri(sym->st_value, rdi(shdrs[newIndex].sh_addr));
                } else if (replacedSections.find(section) != replacedSections.end()) {
                    wri(sym->st_value, rdi(shdrs[newIndex].sh_addr));
                }

...however, that only works for the special case that the symbol points to the beginning of the beginning of the section. What we really want is something that looks like:

   Elf_Addr addr = rdi(sym->st_value);
   addr -= OLD_START_ADDRESS_OF_SECTION;
   addr += rdi(shdrs[newIndex].sh_addr));
   wri(sym->st_value, addr);

I dug around and its not clear if the original address of the section is kept around anywhere though...

from patchelf.

Patryk27 avatar Patryk27 commented on June 1, 2024

Alright, got some progress here!

As it turns out, updating the symbol is not sufficient, because at least GCC-generated code seems to also rely on the .rela.dyn section to fill-in references to the symbol, so patchelf also needs to update that as well - but having done so, it seems that this problem is solved and the program from SO works.

Somewhat unfortunately, this doesn't actually fix the underlying issue with NodeJS (and pcloud, in my case), so on to investigating further 🔍

from patchelf.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.