[Git][ghc/ghc][master] 3 commits: Linker: use m32 allocator for sections when NEED_PLT (#24432)

Marge Bot (@marge-bot) gitlab at gitlab.haskell.org
Fri Jul 12 15:44:08 UTC 2024



Marge Bot pushed to branch master at Glasgow Haskell Compiler / GHC


Commits:
5104ee61 by Sylvain Henry at 2024-07-12T11:43:23-04:00
Linker: use m32 allocator for sections when NEED_PLT (#24432)

Use M32 allocator to avoid fragmentation when allocating ELF sections.
We already did this when NEED_PLT was undefined. Failing to do this led
to relocations impossible to fulfil (#24432).

- - - - -
52d66984 by Sylvain Henry at 2024-07-12T11:43:23-04:00
RTS: allow M32 allocation outside of 4GB range when assuming -fPIC

- - - - -
c34fef56 by Sylvain Henry at 2024-07-12T11:43:23-04:00
Linker: fix stub offset

Remove unjustified +8 offset that leads to memory corruption (cf
discussion in #24432).

- - - - -


4 changed files:

- rts/linker/Elf.c
- rts/linker/M32Alloc.c
- rts/linker/elf_plt.c
- rts/linker/macho/plt.c


Changes:

=====================================
rts/linker/Elf.c
=====================================
@@ -863,25 +863,23 @@ ocGetNames_ELF ( ObjectCode* oc )
 
           unsigned nstubs = numberOfStubsForSection(oc, i);
           unsigned stub_space = STUB_SIZE * nstubs;
+          unsigned full_size = size+stub_space;
 
-          void * mem = mmapAnonForLinker(size+stub_space);
+          // use M32 allocator to avoid fragmentation and relocations impossible
+          // to fulfil (cf #24432)
+          bool executable = kind == SECTIONKIND_CODE_OR_RODATA;
+          m32_allocator *allocator = executable ? oc->rx_m32 : oc->rw_m32;
 
-          if( mem == MAP_FAILED ) {
-            barf("failed to mmap allocated memory to load section %d. "
-                 "errno = %d", i, errno);
-          }
+          // Correctly align the section. This is particularly important for
+          // the alignment of .rodata.cstNN sections.
+          start = m32_alloc(allocator, full_size, align);
+          if (start == NULL) goto fail;
+          alloc = SECTION_M32;
 
           /* copy only the image part over; we don't want to copy data
            * into the stub part.
            */
-          memcpy( mem, oc->image + offset, size );
-
-          alloc = SECTION_MMAP;
-
-          mapped_offset = 0;
-          mapped_size = roundUpToPage(size+stub_space);
-          start = mem;
-          mapped_start = mem;
+          memcpy(start, oc->image + offset, size);
 #else
           if (USE_CONTIGUOUS_MMAP || RtsFlags.MiscFlags.linkerAlwaysPic) {
               // already mapped.
@@ -918,7 +916,7 @@ ocGetNames_ELF ( ObjectCode* oc )
 
 #if defined(NEED_PLT)
           oc->sections[i].info->nstubs = 0;
-          oc->sections[i].info->stub_offset = (uint8_t*)mem + size;
+          oc->sections[i].info->stub_offset = (uint8_t*)start + size;
           oc->sections[i].info->stub_size = stub_space;
           oc->sections[i].info->stubs = NULL;
 #else


=====================================
rts/linker/M32Alloc.c
=====================================
@@ -156,7 +156,10 @@ static bool
 is_okay_address(void *p) {
   int8_t *here = LINKER_LOAD_BASE;
   ssize_t displacement = (int8_t *) p - here;
-  return (displacement > -0x7fffffff) && (displacement < 0x7fffffff);
+  // if we assume -fPIC, we don't care where we load code.
+  // But we still want to use the m32 allocator to avoid fragmentation (#24432)
+  return RtsFlags.MiscFlags.linkerAlwaysPic
+         || ((displacement > -0x7fffffff) && (displacement < 0x7fffffff));
 }
 
 enum m32_page_type {


=====================================
rts/linker/elf_plt.c
=====================================
@@ -56,8 +56,7 @@ makeStub(Section * section,
     s->target = *addr;
     s->flags  = flags;
     s->next = NULL;
-    s->addr = (uint8_t *)section->info->stub_offset + 8
-            + STUB_SIZE * section->info->nstubs;
+    s->addr = (uint8_t *)section->info->stub_offset + STUB_SIZE * section->info->nstubs;
 
     if((*_makeStub)(s))
         return EXIT_FAILURE;


=====================================
rts/linker/macho/plt.c
=====================================
@@ -56,8 +56,7 @@ makeStub(Section * section,
     s->target = *addr;
     s->flags  = flags;
     s->next = NULL;
-    s->addr = (uint8_t *)section->info->stub_offset + 8
-            + STUB_SIZE * section->info->nstubs;
+    s->addr = (uint8_t *)section->info->stub_offset + STUB_SIZE * section->info->nstubs;
 
     if((*_makeStub)(s))
         return EXIT_FAILURE;



View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/compare/dee035bf618d75a18fe72dd3977434c0749a2156...c34fef56367142fa55e9861092f64cc7b9946fa1

-- 
This project does not include diff previews in email notifications.
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/compare/dee035bf618d75a18fe72dd3977434c0749a2156...c34fef56367142fa55e9861092f64cc7b9946fa1
You're receiving this email because of your account on gitlab.haskell.org.


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/ghc-commits/attachments/20240712/2b1a230a/attachment-0001.html>


More information about the ghc-commits mailing list