Search code examples
assemblyavrelf

Getting a .elf file instead of a .s file when running "make buzz.s"


I'm using the following makefile:

 # Makefile for AVR lab1 - delay blink
 TARGET =    buzz
 #AFILES =   buzz.S
 CFILES =    buzz.c

 MCU    =    atmega328p
 F_CPU  =    16000000
 PORT   =    /dev/ttyACM0

 # do not modify anything below this line
 include        ~/lib/AVRmaster.mak

and the following included file:

DCONF =     /usr/share/arduino/hardware/tools/avrdude.conf
OBJS =      $(CFILES:.c=.o) $(AFILES:.S=.o)

CFLAGS =    -Wall
CFLAGS +=   -Os
CFLAGS +=   -mmcu=$(MCU)
CFLAGS +=   -DF_CPU=$(F_CPU)UL
CFLAGS +=   -g$(DEBUG)
CFLAGS +=   -MMD -DUSB_VID=null -DUSB_PID=null
CFLAGS +=   -DARDUINO=105
CFLGS  +=   -D__PROG_TYPES_COMPAT__
CFLAGS +=   -I/usr/share/arduino/hardware/cores/arduino
CFLAGS +=   -I/usr/share/arduino/hardware/arduino/variants/standard
CFLAGS +=   -fno-exceptions
CFLAGS +=   -ffunction-sections
CFLAGS +=   -fdata-sections

AFLAGS  =   -mmcu=$(MCU) -I. -g -x assembler-with-cpp
AFLAGS +=   -DF_CPU=$(F_CPU)
AFLAGS +=   -Wa,-adhlns=$(<:%.S=%.lst),-gstabs,--listing-cont-lines=100

LDFLAGS =   -Wl,-Map=$(TARGET).map,--cref

ifeq     ($(INTCODE), FALSE)
LDFLAGS +=  -nostartfiles
endif

DFLAGS =    -C$(DCONF)
DFLAGS +=   -v -v -v -v
DFLAGS +=   -carduino
DFLAGS +=   -p$(MCU)
DFLAGS +=   -P$(PORT)
DFLAGS +=   -b115200
DFLAGS +=   -D

all:        $(TARGET).hex

%.o:        %.S
            avr-gcc $(AFLAGS) -c $< -o $@

%.o:        %.c
            avr-gcc $(CFLAGS) -c $< -o $@

$(TARGET).elf:      $(OBJS)
            avr-gcc $(CFLAGS) $^ -o $@ $(LDFLAGS)

%.hex:      %.elf
            avr-objcopy -O ihex -j .text -j .data $< $@

%.s:        %.elf
            avr-objdump -D $< > $@

load:       $(TARGET).hex
            avrdude $(DFLAGS) -Uflash:w:$(TARGET).hex:i            

clean:
        rm -f *.hex *.elf *.o *.map *.d *.s *.lst

running

make buzz.s

produces a file with the first of about a thousand lines being

 buzz.elf:     file format elf32-avr


 Disassembly of section .data:

I'm trying to create an assembly language version of the c program I wrote, and am allowed to reverse engineer a fully commented program from the c source code. I'm not sure I'll be able to reverse engineer what is produced!


Solution

  • Running make buzz.s does not give you a .elf file. What it gives you is a plain text file that contains the output of the command avr-objdump -D buzz.elf. This is output is a complete disassembly of the file buzz.elf. The disassembly starts with the .data section, which normally doesn't include any code, so the file starts with data incorrectly disassembled as instructions. Presumably this looks like garbage. If you scroll down to the start of the .text section in this file you should the disassembly of your code, along with any library code linked into the program.

    If you want to see the assembly output of the compiler try adding these lines your makefile, after the include ~/lib/AVRmaster.mak line:

    %.ss: %.c
        avr-gcc $(CFLAGS) -S $< -o $@
    

    There needs to be tab character at the start of the second line. If you cut & paste the above on into your makefile, you'll probaby need to delete the spaces at the start of the second line and replace them with a tab character.

    This will let you use the command make buzz.ss to generate a file named buzz.ss that contains the assembly generated by the compiler.