Fix powerpc64 alignment of .toc section in kernel modules
PATCH 1/1 Fix powerpc64 alignment of .toc section in kernel modules
powerpc64 gcc can generate code that offsets an address, to access part of an object in memory. If the address is a -mcmodel=medium toc pointer relative address then code like the following is possible.
addis r9,r2,var@toc@ha
ld r3,var@toc@l(r9)
ld r4,(var+8)@toc@l(r9)
This works fine so long as var is naturally aligned, *and* r2 is sufficiently aligned. If not, there is a possibility that the offset added to access var+8 wraps over a n*64k+32k boundary. Modules don't have any guarantee that r2 is sufficiently aligned. Moreover, code generated by older compilers generates a .toc section with 2**0 alignment, which can result in relocation failures at module load time even without the wrap problem.
Thus, this patch links modules with an aligned .toc section (Makefile and module.lds changes), and forces alignment for out of tree modules or those without a .toc section (module_64.c changes).
--- arch/powerpc/Makefile | 1 + arch/powerpc/kernel/module.lds | 8 ++++++++ arch/powerpc/kernel/module_64.c | 13 +++++++++---- 3 files changed, 18 insertions(+), 4 deletions(-) create mode 100644 arch/powerpc/kernel/module.lds
arch/powerpc/Makefile +1 -0
@@ -63,6 +63,7 @@ UTS_MACHINE := $(subst $(space),,$(machine-y)) ifdef CONFIG_PPC32 KBUILD_LDFLAGS_MODULE += arch/powerpc/lib/crtsavres.o else +KBUILD_LDFLAGS_MODULE += -T arch/powerpc/kernel/module.lds ifeq ($(call ld-ifversion, -ge, 225000000, y),y) # Have the linker provide sfpr if possible. # There is a corresponding test in arch/powerpc/lib/Makefile
arch/powerpc/kernel/module.lds new file +8 -0
@@ -0,0 +1,8 @@ +/* Force alignment of .toc section. */ +SECTIONS +{ + .toc 0 : ALIGN(256) + { + *(.got .toc) + } +}
arch/powerpc/kernel/module_64.c +9 -4
@@ -339,8 +339,11 @@ int module_frob_arch_sections(Elf64_Ehdr *hdr,char *p;if (strcmp (secstrings + sechdrs[i].sh_name,".stubs" ) ==0 ) me->arch.stubs_section = i; -else if (strcmp (secstrings + sechdrs[i].sh_name,".toc" ) ==0 ) +else if (strcmp (secstrings + sechdrs[i].sh_name,".toc" ) ==0 ) { me->arch.toc_section = i; +if (sechdrs[i].sh_addralign <8 ) + sechdrs[i].sh_addralign =8 ; + }else if (strcmp (secstrings+sechdrs[i].sh_name,"__versions" )==0 )dedotify_versions ((void *)hdr + sechdrs[i].sh_offset, sechdrs[i].sh_size);
@@ -374,11 +377,13 @@ int module_frob_arch_sections(Elf64_Ehdr *hdr, }/* r2 is the TOC pointer: it actually points 0x8000 into the TOC (this -gives the value maximum span in an instruction which uses a signed -offset) */ +* gives the value maximum span in an instruction which uses a signed +* offset). Round down to a 256 byte boundary for the odd case where +* we are setting up r2 without a .toc section. +*/ static inline unsigned long my_r2 (const Elf64_Shdr *sechdrs,struct module *me) { -return sechdrs[me->arch.toc_section].sh_addr +0x8000 ; +return (sechdrs[me->arch.toc_section].sh_addr &-256 ) +0x8000 ; }/* Both low and high 16 bits are added as SIGNED additions, so if low
Signature · 1 line
2.9.5
Michael Ellerman
reviewerHi Desnes,
Am I right that Alan largely wrote this patch?
If so it should probably be From: him, so that he is the author in the git log.
arch/powerpc/Makefile UTS_MACHINE := $(subst $(space),,$(machine-y)) · line 66
Quoted 9 lines · Desnes Augusto Nunes do Rosario
arch/powerpc/Makefile +1
-0@@ -63,6 +63,7 @@ UTS_MACHINE := $(subst $(space),,$(machine-y)) ifdef CONFIG_PPC32 KBUILD_LDFLAGS_MODULE += arch/powerpc/lib/crtsavres.o else +KBUILD_LDFLAGS_MODULE += -T arch/powerpc/kernel/module.lds
This needs to be:
KBUILD_LDFLAGS_MODULE += -T $(srctree)/arch/powerpc/kernel/module.lds
Otherwise building with O=../build fails with:
ld: cannot open linker script file arch/powerpc/kernel/module.lds: No such file or directory
I'll fix it up.
Quoted 8 lines
arch/powerpc/kernel/module_64.c +0
-0@@ -374,11 +377,13 @@ int module_frob_arch_sections(Elf64_Ehdr *hdr, }/* r2 is the TOC pointer: it actually points 0x8000 into the TOC (this
arch/powerpc/kernel/module_64.c int module_frob_arch_sections(Elf64_Ehdr *hdr, · line 386
Quoted 10 lines
arch/powerpc/kernel/module_64.c +5
-3-gives the value maximum span in an instruction which uses a signed -offset) */ +* gives the value maximum span in an instruction which uses a signed +* offset). Round down to a 256 byte boundary for the odd case where +* we are setting up r2 without a .toc section. +*/ static inline unsigned long my_r2 (const Elf64_Shdr *sechdrs,struct module *me) { -return sechdrs[me->arch.toc_section].sh_addr +0x8000 ; +return (sechdrs[me->arch.toc_section].sh_addr &-256 ) +0x8000 ;
I think it's more typical in the kernel to write -256 as ~0xff.
Again I can fix it up.
cheers
Hello Michael,
Quoted 6 lines · Michael Ellerman
Hi Desnes,
Am I right that Alan largely wrote this patch?
If so it should probably be From: him, so that he is the author in the git log.
Yes, Alan Modra is the main author and I am just committing it with minor changes. Thus, the author change is necessary.
Quoted 15 lines
arch/powerpc/Makefile UTS_MACHINE := $(subst $(space),,$(machine-y)) · line 66
Quoted 9 lines · Desnes Augusto Nunes do Rosario
arch/powerpc/Makefile +1
-0@@ -63,6 +63,7 @@ UTS_MACHINE := $(subst $(space),,$(machine-y)) ifdef CONFIG_PPC32 KBUILD_LDFLAGS_MODULE += arch/powerpc/lib/crtsavres.o else +KBUILD_LDFLAGS_MODULE += -T arch/powerpc/kernel/module.ldsThis needs to be:
Quoted 7 lines
KBUILD_LDFLAGS_MODULE+= -T $(srctree)/arch/powerpc/kernel/module.ldsOtherwise building with O=../build fails with:
ld: cannot open linker script file
arch/powerpc/kernel/module.lds: No such file or directoryI'll fix it up.
Indeed; this change is necessary to avoid any path errors.
Quoted 22 lines
Quoted 9 lines
arch/powerpc/kernel/module_64.c +0
-0@@ -374,11 +377,13 @@ int module_frob_arch_sections(Elf64_Ehdr *hdr, }/* r2 is the TOC pointer: it actually points 0x8000 into the TOC (this arch/powerpc/kernel/module_64.c int module_frob_arch_sections(Elf64_Ehdr *hdr, · line 386
Quoted 10 lines
arch/powerpc/kernel/module_64.c +5
-3-gives the value maximum span in an instruction which uses a signed -offset) */ +* gives the value maximum span in an instruction which uses a signed +* offset). Round down to a 256 byte boundary for the odd case where +* we are setting up r2 without a .toc section. +*/ static inline unsigned long my_r2 (const Elf64_Shdr *sechdrs,struct module *me) { -return sechdrs[me->arch.toc_section].sh_addr +0x8000 ; +return (sechdrs[me->arch.toc_section].sh_addr &-256 ) +0x8000 ;I think it's more typical in the kernel to write -256 as ~0xff.
Quoted 1 line
Again I can fix it up.
Good to know!
Quoted 3 lines
cheers
Lastly, will you fix it up or do you want me to send a second version then? Whatever is best for you.
Thank you for the review.
Signature · 8 lines
Desnes Augusto Nunes do Rosário ------------------------------------------ Linux Developer - IBM / Brazil M.Sc. in Electrical and Computer Engineering - UFRN (11) 9595-30-900 [email protected]
Michael Ellerman
reviewerDesnes Augusto Nunes do Ros=C3=A1rio <[email protected]> writes:
...
Quoted 3 lines
Lastly, will you fix it up or do you want me to send a second version=20 then? Whatever is best for you.
No that's fine I've done those changes, no need to send another version.
cheers
Michael Ellerman
AppliedQuoted 16 lines · Desnes Augusto Nunes do Rosario
powerpc64 gcc can generate code that offsets an address, to access part of an object in memory. If the address is a -mcmodel=medium toc pointer relative address then code like the following is possible.
addis r9,r2,var@toc@ha
ld r3,var@toc@l(r9)
ld r4,(var+8)@toc@l(r9)This works fine so long as var is naturally aligned, *and* r2 is sufficiently aligned. If not, there is a possibility that the offset added to access var+8 wraps over a n*64k+32k boundary. Modules don't have any guarantee that r2 is sufficiently aligned. Moreover, code generated by older compilers generates a .toc section with 2**0 alignment, which can result in relocation failures at module load time even without the wrap problem.
Quoted 7 lines
Thus, this patch links modules with an aligned .toc section (
Makefileandmodule.ldschanges), and forces alignment for out of tree modules or those without a .toc section (module_64.cchanges).Signed-off-by Alan Modra <[email protected]>[ desnesn: updated patch to apply to powerpc-next kernel v4.15 ]Signed-off-by Desnes A. Nunes do Rosario <[email protected]>
Applied to powerpc next, thanks.
https://git.kernel.org/powerpc/c/5c45b5280196a92c4437f5648209c5
cheers