[llvm-dev] [LLD] Writing thunks before the corresponding section

Bruce Hoult via llvm-dev llvm-dev at lists.llvm.org
Wed Sep 7 13:50:39 PDT 2016


On Wed, Sep 7, 2016 at 7:55 PM, Peter Smith via llvm-dev <
llvm-dev at lists.llvm.org> wrote:

> Hello Simon,
>
> Yes it is okay to write ARM thunks before an InputSection. There is a
> similar "inline state change" thunk in ARM that does BX PC, NOP to
> change state and fall through.


Maybe it's a little bit evil, but I've found that SUB PC,PC,#3 works just
fine to change to Thumb state without any NOP needed on all
current-generation CPUs I've tried it on, and in particular  Raspberry Pi 2
(Cortex A7), Pi 3 (Cortex A53) and Odroid XU4 (Cortex A15).

Unfortunately I never though to try this ten years ago on the ARM7TDMI

e.g. (assumes Linux EABI kernel)

.equ SYSCALL_EXIT, 1
.equ SYSCALL_WRITE, 4
.equ STDOUT, 1

.globl _start
.syntax unified
_start:
sub pc,pc,#3
.thumb
movs r0,#STDOUT
adr r1,hello
movs r2,#11
movs r7,#SYSCALL_WRITE
swi 0
movs r7,#SYSCALL_EXIT
swi 0

.align 2
hello: .asciz "Hello asm!\n"


It is worth mentioning that disassembly of ARM and Thumb Thunks may
> look a bit strange if they are moved from after the InputSection. This
> is because they lack a mapping symbol ($a or $t) that tells the
> disassembler what instruction set to disassemble. I've got adding
> mapping symbol for linker generated InputSections on my list of things
> to do.
>

This disassembles fine when built in the standard way so there's clearly no
fundamental problem with disassembling past inline thunks:

$ as asm_test.s -o asm_test.o
$ ld asm_test.o -o asm_test
$ ./asm_test
Hello asm!
$ objdump -d asm_test

asm_test:     file format elf32-littlearm

Disassembly of section .text:

00010054 <_start>:
   10054: e24ff003 sub pc, pc, #3
   10058: 2001       movs r0, #1
   1005a: a103       add r1, pc, #12 ; (adr r1, 10068 <hello>)
   1005c: 220b       movs r2, #11
   1005e: 2704       movs r7, #4
   10060: df00       svc 0
   10062: 2701       movs r7, #1
   10064: df00       svc 0
   10066: 46c0       nop ; (mov r8, r8)

00010068 <hello>:
   10068: 6c6c6548 .word 0x6c6c6548
   1006c: 7361206f .word 0x7361206f
   10070: 000a216d .word 0x000a216d

NB that first e24ff003 is an ARM instruction, *not* Thumb2.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20160907/f47ed9ed/attachment.html>


More information about the llvm-dev mailing list