Re: [bootwrapper PATCH v2 11/13] Announce locations of memory objects

Mark Rutland
2022-01-14T16:32:19Z

On Fri, Jan 14, 2022 at 04:04:19PM +0000, Robin Murphy wrote:
> On 2022-01-14 15:30, Andre Przywara wrote:
> > On Fri, 14 Jan 2022 10:56:51 +0000
> > Mark Rutland <mark.rutland@arm.com> wrote:
> > > +void print_ulong_hex(unsigned long val)
> > > +{
> > > + const char hex_chars[16] = "0123456789abcdef";
> >
> > This breaks the build for me (I guess because of the const?):
> > -------------------
> > ld --gc-sections arch/aarch64/boot.o arch/aarch64/stack.o arch/aarch64/utils.o arch/aarch64/init.o arch/aarch64/psci.o common/boot.o common/bakery_lock.o common/platform.o common/lib.o common/init.o common/psci.o common/gic-v3.o -o linux-system.axf --script=model.lds
> > ld: common/platform.o: in function `print_ulong_hex':
> > /src/boot-wrapper-aarch64.git/common/platform.c:58: undefined reference to `__stack_chk_guard'
> > ld: /src/boot-wrapper-aarch64.git/common/platform.c:58: undefined reference to `__stack_chk_guard'
> > ld: /src/boot-wrapper-aarch64.git/common/platform.c:66: undefined reference to `__stack_chk_fail'
> > /src/boot-wrapper-aarch64.git/common/platform.c:66:(.text.print_ulong_hex+0xa0): relocation truncated to fit: R_AARCH64_CALL26 against undefined symbol `__stack_chk_fail'
> > make: *** [Makefile:684: linux-system.axf] Error 1
> > ------------------
>
> I got curious and tried a quick test compiling this function in isolation,
> and indeed for some reason GCC (9.3.0 at -O2) seems to end up allocating 80
> bytes of stack, which is apparently large enough to trigger insertion of a
> stack check, and laboriously copying the string from static data into it.
> FWIW either making hex_chars static const, or simply '#define hex_chars
> "01234567abcdef"', not only avoids a stack check but also leads to notably
> nicer code.

For the stack check, I think Andre's patch to add -fno-stack-protector is the
correct fix, so I'd like to take that as a prerequisite.

To avoid using the stack unnecessarily and to make the code nicer, I'll also
take your suggestion with the fixup below.

Thanks,
Mark.

---->8----
-#define HEX_CHARS_PER_LONG (2 * sizeof(long))
+#define HEX_CHARS_PER_LONG (2 * sizeof(long))
+#define HEX_CHARS "0123456789abcdef"

void print_ulong_hex(unsigned long val)
{
- const char hex_chars[16] = "0123456789abcdef";
int i;

for (i = HEX_CHARS_PER_LONG - 1; i >= 0; i--) {
int v = (val >> (4 * i)) & 0xf;
- print_char(hex_chars[v]);
+ print_char(HEX_CHARS[v]);
}
}