From: Alan Modra <hidden> Date: 2021-03-09 04:57:16
This patch future-proofs the kernel against linker changes that might
put the toc pointer at some location other than .got+0x8000, by
replacing __toc_start+0x8000 with __toc_ptr throughout. __toc_ptr
is set from the symbol .TOC. emitted by ld on a final link, falling
back to .got+0x8000 for older ld that doesn't provide .TOC. If the
kernel's idea of the toc pointer doesn't agree with the linker, bad
things happen.
prom_init.c code relocating its toc is also changed so that a symbolic
__prom_init_toc_start toc-pointer relative address is calculated
rather than assuming that it is always at toc-pointer - 0x8000. The
length calculations loading values from the toc are also avoided.
It's a little incestuous to do that with unreloc_toc picking up
adjusted values (which is fine in practice, they both adjust by the
same amount if all goes well).
I've also changed the way .got is aligned in vmlinux.lds and
zImage.lds, mostly so that dumping out section info by objdump or
readelf plainly shows the alignment is 256. This linker script
feature was added 2005-09-27, available in FSF binutils releases from
2.17 onwards. Should be safe to use in the kernel, I think.
Finally, put *(.got) before the prom_init.o entry which only needs
*(.toc), so that the GOT header goes in the correct place. I don't
believe this makes any difference for the kernel as it would for
dynamic objects being loaded by ld.so. That change is just to stop
lusers who blindly copy kernel scripts being led astray. Of course,
this change needs the prom_init.c changes.
Some notes on .toc and .got.
.toc is a compiler generated section of addresses. .got is a linker
generated section of addresses, generally built when the linker sees
R_*_*GOT* relocations. In the case of powerpc64 ld.bfd, there are
multiple generated .got sections, one per input object file. So you
can somewhat reasonably write in a linker script an input section
statement like *prom_init.o(.got .toc) to mean "the .got and .toc
section for files matching *prom_init.o". On other architectures that
doesn't make sense, because the linker generally has just one .got
section. Even on powerpc64, note well that the GOT entries for
prom_init.o may be merged with GOT entries from other objects. That
means that if prom_init.o references, say, _end via some GOT
relocation, and some other object also references _end via a GOT
relocation, the GOT entry for _end may be in the range
__prom_init_toc_start to __prom_init_toc_end and if the kernel does
something special to GOT/TOC entries in that range then the value of
_end as seen by objects other than prom_init.o will be affected. On
the other hand the GOT entry for _end may not be in the range
__prom_init_toc_start to __prom_init_toc_end. Which way it turns out
is deterministic but a detail of linker operation that should not be
relied on.
A feature of ld.bfd is that input .toc (and .got) sections matching
one linker input section statement may be sorted, to put entries used
by small-model code first, near the toc base. This is why scripts for
powerpc64 normally use *(.got .toc) rather than *(.got) *(.toc), since
the first form allows more freedom to sort.
Another feature of ld.bfd is that indirect addressing sequences using
the GOT/TOC may be edited by the linker to relative addressing. In
many cases relative addressing would be emitted by gcc for
-mcmodel=medium if you appropriately decorate variable declarations
with non-default visibility.
@@ -49,13 +49,13 @@ static inline int in_kernel_text(unsigned long addr)staticinlineunsignedlongkernel_toc_addr(void){/* Defined by the linker, see vmlinux.lds.S */-externunsignedlong__toc_start;+externunsignedlong__toc_ptr;/**TheTOCregister(r2)points32kBintotheTOC,sothat64kBof*theTOCcanbeaddressedusingasinglemachineinstruction.*/-return(unsignedlong)(&__toc_start)+0x8000UL;+return(unsignedlong)&__toc_ptr;}staticinlineintoverlaps_interrupt_vector_text(unsignedlongstart,
@@ -3220,27 +3220,26 @@ static void unreloc_toc(void){}#else-staticvoid__reloc_toc(unsignedlongoffset,unsignedlongnr_entries)+staticvoid__reloc_toc(unsignedlongoffset){-unsignedlongi;unsignedlong*toc_entry;+unsignedlong*toc_start,*toc_end;-/* Get the start of the TOC by using r2 directly. */-asmvolatile("addi %0,2,-0x8000":"=b"(toc_entry));+asm("addis %0,2,__prom_init_toc_start@toc@ha\n\t"+"addi %0,%0,__prom_init_toc_start@toc@l":"=b"(toc_start));+asm("addis %0,2,__prom_init_toc_end@toc@ha\n\t"+"addi %0,%0,__prom_init_toc_end@toc@l":"=b"(toc_end));-for(i=0;i<nr_entries;i++){-*toc_entry=*toc_entry+offset;-toc_entry++;+for(toc_entry=toc_start;toc_entry!=toc_end;toc_entry++){+*toc_entry+=offset;}}staticvoidreloc_toc(void){unsignedlongoffset=reloc_offset();-unsignedlongnr_entries=-(__prom_init_toc_end-__prom_init_toc_start)/sizeof(long);-__reloc_toc(offset,nr_entries);+__reloc_toc(offset);mb();}
From: Alan Modra <hidden> Date: 2021-03-10 03:48:51
This patch future-proofs the kernel against linker changes that might
put the toc pointer at some location other than .got+0x8000, by
replacing __toc_start+0x8000 with .TOC. throughout. If the kernel's
idea of the toc pointer doesn't agree with the linker, bad things
happen.
prom_init.c code relocating its toc is also changed so that a symbolic
__prom_init_toc_start toc-pointer relative address is calculated
rather than assuming that it is always at toc-pointer - 0x8000. The
length calculations loading values from the toc are also avoided.
It's a little incestuous to do that with unreloc_toc picking up
adjusted values (which is fine in practice, they both adjust by the
same amount if all goes well).
I've also changed the way .got is aligned in vmlinux.lds and
zImage.lds, mostly so that dumping out section info by objdump or
readelf plainly shows the alignment is 256. This linker script
feature was added 2005-09-27, available in FSF binutils releases from
2.17 onwards. Should be safe to use in the kernel, I think.
Finally, put *(.got) before the prom_init.o entry which only needs
*(.toc), so that the GOT header goes in the correct place. I don't
believe this makes any difference for the kernel as it would for
dynamic objects being loaded by ld.so. That change is just to stop
lusers who blindly copy kernel scripts being led astray. Of course,
this change needs the prom_init.c changes.
Some notes on .toc and .got.
.toc is a compiler generated section of addresses. .got is a linker
generated section of addresses, generally built when the linker sees
R_*_*GOT* relocations. In the case of powerpc64 ld.bfd, there are
multiple generated .got sections, one per input object file. So you
can somewhat reasonably write in a linker script an input section
statement like *prom_init.o(.got .toc) to mean "the .got and .toc
section for files matching *prom_init.o". On other architectures that
doesn't make sense, because the linker generally has just one .got
section. Even on powerpc64, note well that the GOT entries for
prom_init.o may be merged with GOT entries from other objects. That
means that if prom_init.o references, say, _end via some GOT
relocation, and some other object also references _end via a GOT
relocation, the GOT entry for _end may be in the range
__prom_init_toc_start to __prom_init_toc_end and if the kernel does
something special to GOT/TOC entries in that range then the value of
_end as seen by objects other than prom_init.o will be affected. On
the other hand the GOT entry for _end may not be in the range
__prom_init_toc_start to __prom_init_toc_end. Which way it turns out
is deterministic but a detail of linker operation that should not be
relied on.
A feature of ld.bfd is that input .toc (and .got) sections matching
one linker input section statement may be sorted, to put entries used
by small-model code first, near the toc base. This is why scripts for
powerpc64 normally use *(.got .toc) rather than *(.got) *(.toc), since
the first form allows more freedom to sort.
Another feature of ld.bfd is that indirect addressing sequences using
the GOT/TOC may be edited by the linker to relative addressing. In
many cases relative addressing would be emitted by gcc for
-mcmodel=medium if you appropriately decorate variable declarations
with non-default visibility.
Signed-off-by: Alan Modra <redacted>
@@ -48,14 +48,18 @@ static inline int in_kernel_text(unsigned long addr)staticinlineunsignedlongkernel_toc_addr(void){-/* Defined by the linker, see vmlinux.lds.S */-externunsignedlong__toc_start;--/*-*TheTOCregister(r2)points32kBintotheTOC,sothat64kBof-*theTOCcanbeaddressedusingasinglemachineinstruction.-*/-return(unsignedlong)(&__toc_start)+0x8000UL;+#if 0+/* This version is appropriate if the kernel is never compiled+-mcmodel=smallorthetotal.tocisalwayslessthan64k.*/+registerunsignedlongtoc_ptrasm("r2");+returntoc_ptr;+#else+/* Otherwise linker automatic multiple toc sections might be+required,andinthatcaser2maybeadjustedbyalinker+stub.*/+externunsignedlong__attribute__((visibility("hidden")))tocasm(".TOC.");+return(unsignedlong)&toc;+#endif}staticinlineintoverlaps_interrupt_vector_text(unsignedlongstart,
@@ -3220,27 +3220,26 @@ static void unreloc_toc(void){}#else-staticvoid__reloc_toc(unsignedlongoffset,unsignedlongnr_entries)+staticvoid__reloc_toc(unsignedlongoffset){-unsignedlongi;unsignedlong*toc_entry;+unsignedlong*toc_start,*toc_end;-/* Get the start of the TOC by using r2 directly. */-asmvolatile("addi %0,2,-0x8000":"=b"(toc_entry));+asm("addis %0,2,__prom_init_toc_start@toc@ha\n\t"+"addi %0,%0,__prom_init_toc_start@toc@l":"=b"(toc_start));+asm("addis %0,2,__prom_init_toc_end@toc@ha\n\t"+"addi %0,%0,__prom_init_toc_end@toc@l":"=b"(toc_end));-for(i=0;i<nr_entries;i++){-*toc_entry=*toc_entry+offset;-toc_entry++;+for(toc_entry=toc_start;toc_entry!=toc_end;toc_entry++){+*toc_entry+=offset;}}staticvoidreloc_toc(void){unsignedlongoffset=reloc_offset();-unsignedlongnr_entries=-(__prom_init_toc_end-__prom_init_toc_start)/sizeof(long);-__reloc_toc(offset,nr_entries);+__reloc_toc(offset);mb();}
From: Alan Modra <hidden> Date: 2021-03-10 05:07:53
On Wed, Mar 10, 2021 at 03:44:44PM +1100, Alexey Kardashevskiy wrote:
For my own education, is .got for prom_init.o still generated by ld or gcc?
.got is generated by ld.
In other words, should "objdump -D -s -j .got" ever dump .got for any .o
file, like below?
No. "objdump -r prom_init.o | grep GOT" will tell you whether
prom_init.o *may* cause ld to generate .got entries. (Linker
optimisations or --gc-sections might remove the need for those .got
entries.)
objdump: section '.got' mentioned in a -j option, but not found in any input
file
Right, expected.
--
Alan Modra
Australia Development Lab, IBM
This patch future-proofs the kernel against linker changes that might
put the toc pointer at some location other than .got+0x8000, by
replacing __toc_start+0x8000 with .TOC. throughout. If the kernel's
idea of the toc pointer doesn't agree with the linker, bad things
happen.
Works great with gcc (v8, v10), ld (2.23), clang-11, lld-11.
prom_init.c code relocating its toc is also changed so that a symbolic
__prom_init_toc_start toc-pointer relative address is calculated
rather than assuming that it is always at toc-pointer - 0x8000. The
length calculations loading values from the toc are also avoided.
It's a little incestuous to do that with unreloc_toc picking up
adjusted values (which is fine in practice, they both adjust by the
same amount if all goes well).
I've also changed the way .got is aligned in vmlinux.lds and
zImage.lds, mostly so that dumping out section info by objdump or
readelf plainly shows the alignment is 256. This linker script
feature was added 2005-09-27, available in FSF binutils releases from
2.17 onwards. Should be safe to use in the kernel, I think.
Finally, put *(.got) before the prom_init.o entry which only needs
*(.toc), so that the GOT header goes in the correct place. I don't
believe this makes any difference for the kernel as it would for
dynamic objects being loaded by ld.so. That change is just to stop
lusers who blindly copy kernel scripts being led astray. Of course,
this change needs the prom_init.c changes.
Some notes on .toc and .got.
.toc is a compiler generated section of addresses. .got is a linker
generated section of addresses, generally built when the linker sees
R_*_*GOT* relocations. In the case of powerpc64 ld.bfd, there are
multiple generated .got sections, one per input object file. So you
can somewhat reasonably write in a linker script an input section
statement like *prom_init.o(.got .toc) to mean "the .got and .toc
section for files matching *prom_init.o".
For my own education, is .got for prom_init.o still generated by ld or gcc?
In other words, should "objdump -D -s -j .got" ever dump .got for any .o
file, like below?
===
objdump -D -s -j .got
~/pbuild/kernel-llvm-ld/arch/powerpc/kernel/prom_init.o
/home/aik/pbuild/kernel-llvm-ld/arch/powerpc/kernel/prom_init.o:
file format elf64-powerpcle
objdump: section '.got' mentioned in a -j option, but not found in any
input file
===
--
Alexey Kardashevskiy
IBM OzLabs, LTC Team
e-mail: aik@linux.ibm.com
One more question - the older version had a construct "DEFINED (.TOC.) ?
.TOC. : ..." in case .TOC. is not defined (too old ld? too old gcc?) but
the newer patch seems assuming it is always defined, when was it added?
I have the same check in SLOF, for example, do I still need it?
On 10/03/2021 16:07, Alan Modra wrote:
On Wed, Mar 10, 2021 at 03:44:44PM +1100, Alexey Kardashevskiy wrote:
quoted
For my own education, is .got for prom_init.o still generated by ld or gcc?
.got is generated by ld.
quoted
In other words, should "objdump -D -s -j .got" ever dump .got for any .o
file, like below?
No. "objdump -r prom_init.o | grep GOT" will tell you whether
prom_init.o *may* cause ld to generate .got entries. (Linker
optimisations or --gc-sections might remove the need for those .got
entries.)
quoted
objdump: section '.got' mentioned in a -j option, but not found in any input
file
Right, expected.
--
Alexey Kardashevskiy
IBM OzLabs, LTC Team
e-mail: aik@linux.ibm.com
From: Alan Modra <hidden> Date: 2021-03-10 12:25:56
On Wed, Mar 10, 2021 at 08:33:37PM +1100, Alexey Kardashevskiy wrote:
One more question - the older version had a construct "DEFINED (.TOC.) ?
.TOC. : ..." in case .TOC. is not defined (too old ld? too old gcc?) but the
newer patch seems assuming it is always defined, when was it added? I have
the same check in SLOF, for example, do I still need it?
.TOC. symbol support was first added 2012-11-06, so you need
binutils-2.24 or later to use .TOC. as a symbol.
--
Alan Modra
Australia Development Lab, IBM
On Wed, Mar 10, 2021 at 08:33:37PM +1100, Alexey Kardashevskiy wrote:
quoted
One more question - the older version had a construct "DEFINED (.TOC.) ?
.TOC. : ..." in case .TOC. is not defined (too old ld? too old gcc?) but the
newer patch seems assuming it is always defined, when was it added? I have
the same check in SLOF, for example, do I still need it?
.TOC. symbol support was first added 2012-11-06, so you need
binutils-2.24 or later to use .TOC. as a symbol.
On Wed, Mar 10, 2021 at 01:44:57PM +0100, Christophe Leroy wrote:
Le 10/03/2021 à 13:25, Alan Modra a écrit :
quoted
On Wed, Mar 10, 2021 at 08:33:37PM +1100, Alexey Kardashevskiy wrote:
quoted
One more question - the older version had a construct "DEFINED (.TOC.) ?
.TOC. : ..." in case .TOC. is not defined (too old ld? too old gcc?) but
the
newer patch seems assuming it is always defined, when was it added? I have
the same check in SLOF, for example, do I still need it?
.TOC. symbol support was first added 2012-11-06, so you need
binutils-2.24 or later to use .TOC. as a symbol.
The minimum GCC version required is 4.9, released April 2014, so it
would make sense to require binutils 2.24 at least as well: that was the
last binutils release before the GCC 4.9 release (it was end of 2013).
Generally you should make sure to always have a binutils at least as new
as your GCC (and newer almost always works just fine).
Segher
From: Alan Modra <hidden> Date: 2021-03-10 23:42:14
On Wed, Mar 10, 2021 at 01:44:57PM +0100, Christophe Leroy wrote:
Le 10/03/2021 à 13:25, Alan Modra a écrit :
quoted
On Wed, Mar 10, 2021 at 08:33:37PM +1100, Alexey Kardashevskiy wrote:
quoted
One more question - the older version had a construct "DEFINED (.TOC.) ?
.TOC. : ..." in case .TOC. is not defined (too old ld? too old gcc?) but the
newer patch seems assuming it is always defined, when was it added? I have
the same check in SLOF, for example, do I still need it?
.TOC. symbol support was first added 2012-11-06, so you need
binutils-2.24 or later to use .TOC. as a symbol.
Yes, and arch/powerpc/Makefile complains about 2.24. So for powerpc
that means you need to go to at least 2.25. Oh the horror of needing
such new tools!
--
Alan Modra
Australia Development Lab, IBM
From: Michael Ellerman <hidden> Date: 2021-03-11 23:33:22
Alan Modra [off-list ref] writes:
On Wed, Mar 10, 2021 at 01:44:57PM +0100, Christophe Leroy wrote:
quoted
Le 10/03/2021 à 13:25, Alan Modra a écrit :
quoted
On Wed, Mar 10, 2021 at 08:33:37PM +1100, Alexey Kardashevskiy wrote:
quoted
One more question - the older version had a construct "DEFINED (.TOC.) ?
.TOC. : ..." in case .TOC. is not defined (too old ld? too old gcc?) but the
newer patch seems assuming it is always defined, when was it added? I have
the same check in SLOF, for example, do I still need it?
.TOC. symbol support was first added 2012-11-06, so you need
binutils-2.24 or later to use .TOC. as a symbol.
Yes, and arch/powerpc/Makefile complains about 2.24. So for powerpc
that means you need to go to at least 2.25.
Not quite. It only complains for little endian builds, and only if you
have stock 2.24, it will allow a 2.24.<something>.
I do most of my builds with 2.34, so I have no issue with newer
binutils. But we try not to increase the minimum version too rapidly to
accommodate folks using older and/or "Enterprise" distros that are stuck
on old toolchains.
I think we are within our rights to increase the minimum requirement for
powerpc builds, if it brings advantages we can identify.
The way to do that would be to add a new check in our arch Makefile that
rejects the older versions.
cheers
On Wed, Mar 10, 2021 at 01:44:57PM +0100, Christophe Leroy wrote:
quoted
Le 10/03/2021 à 13:25, Alan Modra a écrit :
quoted
On Wed, Mar 10, 2021 at 08:33:37PM +1100, Alexey Kardashevskiy wrote:
quoted
One more question - the older version had a construct "DEFINED (.TOC.) ?
.TOC. : ..." in case .TOC. is not defined (too old ld? too old gcc?) but the
newer patch seems assuming it is always defined, when was it added? I have
the same check in SLOF, for example, do I still need it?
.TOC. symbol support was first added 2012-11-06, so you need
binutils-2.24 or later to use .TOC. as a symbol.
Yes, and arch/powerpc/Makefile complains about 2.24. So for powerpc
that means you need to go to at least 2.25.
Not quite. It only complains for little endian builds, and only if you
have stock 2.24, it will allow a 2.24.<something>.
I do most of my builds with 2.34, so I have no issue with newer
binutils. But we try not to increase the minimum version too rapidly to
accommodate folks using older and/or "Enterprise" distros that are stuck
on old toolchains.
I think we are within our rights to increase the minimum requirement for
powerpc builds, if it brings advantages we can identify.
The way to do that would be to add a new check in our arch Makefile that
rejects the older versions.