public inbox for binutils@sourceware.org
 help / color / mirror / Atom feed
From: Alexandre Oliva <oliva@adacore.com>
To: binutils@sourceware.org
Subject: impredictable alignment on ARM
Date: Tue, 18 Feb 2020 01:21:00 -0000	[thread overview]
Message-ID: <oreeus8zcn.fsf@livre.home> (raw)

Consider the following asm input:

.thumb
.text
ldr r1, 0f
0f: .word 0x12345678

In this case, we report the word is misaligned and fail, though the
section is aligned to 2-byte boundaries, so the word *might* be properly
aligned, after all, if only the previous linked section enabled the text
section above to start 2 bytes after a 4-byte aligned address.  Anyway,
we probably don't want to worry about this case.

However, I think we should be concerned about the converse case:

.thumb
.text
ldr r1, 0f
ldr r2, 1f
0f: .word 0x01234567
1f: .word 0x89abcdef
nop

We do NOT report an error here, but if this text segment gets placed at
a 2-byte offset from a 4-byte aligned address (e.g., link the object
file in twice), the second pair will have misaligned words, and the
PC-relative offsets will resolve to aligned words that contain only part
of the word to be loaded.


The following patchlet arranges for us to complain when the target of
such an ldr doesn't ensure the expected alignment.  However, it's not
quite enough to solve the general problem.  Consider:

.thumb
.text
ldr sp, 0f
0f: .word 0x80000000

This extended form of ldr takes 4 bytes, and it doesn't require nor
ensure the target word to be aligned to a 4-byte boundary.  It just so
happens that, if it's not aligned, the value loaded into the register is
a rotated version of the word containing the misaligned address.

I'm not sure it would be appropriate for us to reject potentially
misaligned words: there might be (obfuscated) code intended to detect
and behave differently depending on whether it ends up at an even or odd
half-word.

However, I think it would be nice for us to at least warn that the code
might behave differently depending on the actual alignment it gets.  I'm
thinking something as simple as tracking the max natural alignment used
in each segment, and warning of potental linker-induced behavior changes
if that alignment is not recorded for the segment.

Tracking symbols with their natural alignments, and maybe even
references to them that expect a certain alignment, might be pushing too
far, on the one hand, and still missing relevant cases of separate
compilation or complex address computations on the other.

Is this something we might want to pursue, so as to warn even for e.g.:

.text
.word 0

but limited to once per segment?

Or should we track and warn about PC-relative addressing requirements,
so as to warn about segments containing PC-relative addressing (in
whatever forms) whose expected alignment exceeds the section's?  (this
could miss e.g. setting a register to PC + offset, and then loading a
word at the address stored in the register)

A combination of these?

Thoughts?


Here's the patchlet that covers only the PCrel-load-to-low-reg case:

--- gas/config/tc-arm.c	2020-01-28 12:50:34.000000000 +0100
+++ gas/config/tc-arm.c	2020-02-18 00:13:11.486184639 +0100
@@ -28755,6 +28755,9 @@
 			  (((unsigned long) fixP->fx_frag->fr_address
 			    + (unsigned long) fixP->fx_where) & ~3)
 			  + (unsigned long) value);
+	  else if (get_recorded_alignment (seg) < 2)
+	    as_warn_where (fixP->fx_file, fixP->fx_line,
+			   _("segment does not ensure enough alignment for target word"));
 
 	  if (value & ~0x3fc)
 	    as_bad_where (fixP->fx_file, fixP->fx_line,


-- 
Alexandre Oliva, freedom fighter   he/him   https://FSFLA.org/blogs/lxo
Free Software Evangelist           Stallman was right, but he's left :(
GNU Toolchain Engineer    FSMatrix: It was he who freed the first of us
FSF & FSFLA board member                The Savior shall return (true);

             reply	other threads:[~2020-02-18  1:21 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-02-18  1:21 Alexandre Oliva [this message]
2020-03-03 15:06 ` Richard Earnshaw (lists)

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=oreeus8zcn.fsf@livre.home \
    --to=oliva@adacore.com \
    --cc=binutils@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).