Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't create elf file directly on ARM? #65

Closed
HPCguy opened this issue Mar 20, 2021 · 9 comments
Closed

Can't create elf file directly on ARM? #65

HPCguy opened this issue Mar 20, 2021 · 9 comments

Comments

@HPCguy
Copy link
Contributor

HPCguy commented Mar 20, 2021

pi@anchor:~/Languages/amacc $ uname -a
Linux anchor 5.10.17-v7l+ #1403 SMP Mon Feb 22 11:33:35 GMT 2021 armv7l GNU/Linux

pi@anchor:~/Languages/amacc $ gcc amacc.c -ldl

pi@anchor:~/Languages/amacc $ cat count.c
int main()
{
int i;
for (i=0; i<10; ++i)
printf("%d\n", i) ;
return i;
}

pi@anchor:~/Languages/amacc $ ./a.out -o foo.o count.c

pi@anchor:~/Languages/amacc $ file foo.o
foo.o: ELF 32-bit LSB executable, ARM, EABI5 version 1 (SYSV), dynamically linked, interpreter /lib/ld-linux-armhf.so.3, stripped

pi@anchor:~/Languages/amacc $ objdump -d foo.o
objdump: foo.o: file format not recognized

??? --- Why doesn't this work?

pi@anchor:~/Languages/amacc $ ./a.out count.c
0
1
2
3
4
5
6
7
8
9
pi@anchor:~/Languages/amacc $

@HPCguy HPCguy changed the title Can't run directly on ARM? Can't create elf file directly on ARM? Mar 20, 2021
@jserv
Copy link
Owner

jserv commented Mar 20, 2021

AMaCC can generate ELF files on the ARMv7-A devices, and @splasky did verify the functionality. Can you simplify the steps to reproduce?

@HPCguy
Copy link
Contributor Author

HPCguy commented Mar 20, 2021

gcc amacc.c -ldl
./a.out -o foo.o count.c
objdump -d foo.o

None of the objdump or nm options seem to work on foo.o . I have even tried the Big Endian and Little Endian options. The object code generated is clearly ok though, because the Jit compile executes properly, as shown in my first post.

Thanks.

@lecopzer
Copy link
Contributor

The elf generated by Amacc hasn't supported objdump yet.
You can commit a PR for this feature.

@jserv
Copy link
Owner

jserv commented Mar 30, 2021

Let's close this issue and concentrate on further tooling ones.

@jserv jserv closed this as completed Mar 30, 2021
@HPCguy
Copy link
Contributor Author

HPCguy commented May 22, 2021

For anyone who wants to disassemble the ELF executable file that amacc produces, this hack works for everything but relocatable addresses:

#!/bin/bash
dd bs=1 if=$1 of=$1.obj `readelf -a $1 2>/dev/null | awk '/\.text.*PROGBITS/ { s = sprintf("skip=%d", "0x" $6) ; c = sprintf("count=%d", "0x" $7) ; print s, c }'`
objdump -b binary -m arm -D $1.obj

@jserv
Copy link
Owner

jserv commented May 24, 2021

For anyone who wants to disassemble the ELF executable file that amacc produces, this hack works for everything but relocatable addresses:

@HPCguy, I failed to utilize the above script to disassemble the ELF file generated by AMaCC. Only the following message:

0+0 records in
0+0 records out
0 bytes copied, 0.000123291 s, 0.0 kB/s

@HPCguy
Copy link
Contributor Author

HPCguy commented May 25, 2021

Hi,

The Issues editor won't allow me to paste the bash script as needed, although when I cut and paste out of the issue window into a script, it is working for me. I can create a pull request containing just the script, if you need it.

Here is what I get for that disasm script:

fact.c:
int fact(int n)
{
int i ;
int retVal = 1 ;
for (i=2; i<=n; ++i)
retVal *= i ;
return retVal ;
}

int main(int argc, char *argv[])
{
return fact(10) ;
}

% ./amacc -o foo fact.c
% ./disasm foo
Disassembly of section .data:

00000000 <.data>:
0: e3a0b000 mov fp, #0
4: e3a0e000 mov lr, #0
8: e49d1004 pop {r1} ; (ldr r1, [sp], #4)
c: e1a0200d mov r2, sp
10: e52d2004 push {r2} ; (str r2, [sp, #-4]!)
14: e52d0004 push {r0} ; (str r0, [sp, #-4]!)
18: e3a0c000 mov ip, #0
1c: e52dc004 push {ip} ; (str ip, [sp, #-4]!)
20: e28f0010 add r0, pc, #16
24: e3a03000 mov r3, #0
28: eb000092 bl 0x278
2c: e3a0007f mov r0, #127 ; 0x7f
30: e3a07001 mov r7, #1
34: ef000000 svc 0x00000000
38: e92d5ff0 push {r4, r5, r6, r7, r8, r9, sl, fp, ip, lr}
3c: e52d0004 push {r0} ; (str r0, [sp, #-4]!)
40: e52d1004 push {r1} ; (str r1, [sp, #-4]!)
44: eb000033 bl 0x118
48: e28dd008 add sp, sp, #8
4c: e8bd9ff0 pop {r4, r5, r6, r7, r8, r9, sl, fp, ip, pc}
50: e92d4800 push {fp, lr}
54: e28db000 add fp, sp, #0
58: e24dd008 sub sp, sp, #8
5c: e24b0008 sub r0, fp, #8
60: e52d0004 push {r0} ; (str r0, [sp, #-4]!)
64: e3a00001 mov r0, #1
68: e49d1004 pop {r1} ; (ldr r1, [sp], #4)
6c: e5810000 str r0, [r1]
70: e24b0004 sub r0, fp, #4
74: e52d0004 push {r0} ; (str r0, [sp, #-4]!)
78: e3a00002 mov r0, #2
7c: e49d1004 pop {r1} ; (ldr r1, [sp], #4)
80: e5810000 str r0, [r1]
84: e24b0004 sub r0, fp, #4
88: e5900000 ldr r0, [r0]
8c: e52d0004 push {r0} ; (str r0, [sp, #-4]!)
90: e28b0008 add r0, fp, #8
94: e5900000 ldr r0, [r0]
98: e49d1004 pop {r1} ; (ldr r1, [sp], #4)
9c: e1510000 cmp r1, r0
a0: c3a00000 movgt r0, #0
a4: d3a00001 movle r0, #1
a8: e3500000 cmp r0, #0
ac: 0a000015 beq 0x108
b0: ea000009 b 0xdc
b4: e24b0004 sub r0, fp, #4
b8: e52d0004 push {r0} ; (str r0, [sp, #-4]!)
bc: e5900000 ldr r0, [r0]
c0: e52d0004 push {r0} ; (str r0, [sp, #-4]!)
c4: e3a00001 mov r0, #1
c8: e49d1004 pop {r1} ; (ldr r1, [sp], #4)
cc: e0800001 add r0, r0, r1
d0: e49d1004 pop {r1} ; (ldr r1, [sp], #4)
d4: e5810000 str r0, [r1]
d8: eaffffe9 b 0x84
dc: e24b0008 sub r0, fp, #8
e0: e52d0004 push {r0} ; (str r0, [sp, #-4]!)
e4: e5900000 ldr r0, [r0]
e8: e52d0004 push {r0} ; (str r0, [sp, #-4]!)
ec: e24b0004 sub r0, fp, #4
f0: e5900000 ldr r0, [r0]
f4: e49d1004 pop {r1} ; (ldr r1, [sp], #4)
f8: e0000091 mul r0, r1, r0
fc: e49d1004 pop {r1} ; (ldr r1, [sp], #4)
100: e5810000 str r0, [r1]
104: eaffffea b 0xb4
108: e24b0008 sub r0, fp, #8
10c: e5900000 ldr r0, [r0]
110: e28bd000 add sp, fp, #0
114: e8bd8800 pop {fp, pc}
118: e92d4800 push {fp, lr}
11c: e28db000 add fp, sp, #0
120: e3a0000a mov r0, #10
124: e52d0004 push {r0} ; (str r0, [sp, #-4]!)
128: ebffffc8 bl 0x50
12c: e28dd004 add sp, sp, #4
130: e28bd000 add sp, fp, #0
134: e8bd8800 pop {fp, pc}

Seeing the code allowed me to start adding optimizations to the backend, so the disasm functionality would likely be generally useful for others.

@HPCguy
Copy link
Contributor Author

HPCguy commented May 25, 2021

PS Here is the optimized code, although I had to add a peephole optimizer to the backend assembly, even after the register assignment changes in codegen(). I haven't wrapped my head around the code enough to make it cleaner. There is obviously even more room for peephole optimizations looking at the code below. I left the old addresses on so you can do an sdiff.

0: e3a0b000 mov fp, #0
4: e3a0e000 mov lr, #0
8: e49d1004 pop {r1} ; (ldr r1, [sp], #4)
c: e1a0200d mov r2, sp
10: e52d2004 push {r2} ; (str r2, [sp, #-4]!)
14: e52d0004 push {r0} ; (str r0, [sp, #-4]!)
18: e3a0c000 mov ip, #0
1c: e52dc004 push {ip} ; (str ip, [sp, #-4]!)
20: e28f0010 add r0, pc, #16
24: e3a03000 mov r3, #0
28: eb00008e bl 0x268
2c: e3a0007f mov r0, #127 ; 0x7f
30: e3a07001 mov r7, #1
34: ef000000 svc 0x00000000
38: e92d5ff0 push {r4, r5, r6, r7, r8, r9, sl, fp, ip, lr}
3c: e52d0004 push {r0} ; (str r0, [sp, #-4]!)
40: e52d1004 push {r1} ; (str r1, [sp, #-4]!)
44: eb00002f bl 0x108
48: e28dd008 add sp, sp, #8
4c: e8bd9ff0 pop {r4, r5, r6, r7, r8, r9, sl, fp, ip, pc}
50: e92d4800 push {fp, lr}
54: e28db000 add fp, sp, #0
58: e24dd008 sub sp, sp, #8
64: e3a00001 mov r0, #1
6c: e5810000 str r0, [fp, #-8]
78: e3a00002 mov r0, #2
80: e5810000 str r0, [fp, #-4]
84: e51b0004 ldr r0, [fp, #-4]
88: e52d0004 push {r0} ; (str r0, [sp, #-4]!)
8c: e59b0008 ldr r0, [fp, #8]
90: e49d2004 pop {r2} ; (ldr r2, [sp], #4)
94: e1520000 cmp r2, r0
98: c3a00000 movgt r0, #0
9c: d3a00001 movle r0, #1
a0: e3500000 cmp r0, #0
a4: 0a000014 beq 0xfc
a8: ea000009 b 0xdc
b4: e5910000 ldr r0, [fp, #-4]
b8: e52d0004 push {r0} ; (str r0, [sp, #-4]!)
bc: e3a00001 mov r0, #1
c0: e49d2004 pop {r2} ; (ldr r2, [sp], #4)
c4: e0800002 add r0, r0, r2
cc: e5810000 str r0, [fp, #-4]
d0: eaffffeb b 0x84
dc: e5910000 ldr r0, [fp, #-8]
e0: e52d0004 push {r0} ; (str r0, [sp, #-4]!)
e4: e51b0004 ldr r0, [fp, #-4]
e8: e49d2004 pop {r2} ; (ldr r2, [sp], #4)
ec: e0000092 mul r0, r2, r0
f4: e5810000 str r0, [fp, #-8]
f8: eaffffeb b 0xb4
fc: e51b0008 ldr r0, [fp, #-8]
100: e28bd000 add sp, fp, #0
104: e8bd8800 pop {fp, pc}
108: e92d4800 push {fp, lr}
10c: e28db000 add fp, sp, #0
110: e3a0000a mov r0, #10
114: e52d0004 push {r0} ; (str r0, [sp, #-4]!)
118: ebffffcc bl 0x50
11c: e28dd004 add sp, sp, #4
120: e28bd000 add sp, fp, #0
124: e8bd8800 pop {fp, pc}

@jserv
Copy link
Owner

jserv commented Jun 7, 2021

The Issues editor won't allow me to paste the bash script as needed, although when I cut and paste out of the issue window into a script, it is working for me. I can create a pull request containing just the script, if you need it.

@HPCguy, Great work! Please send pull request(s). You can create a new directory scripts and put the shell script files there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants