avr-gcc compiles too large optiboot hex to suit - gcc

I'd like to modify Arduino Uno's bootloader (optiboot) in order to increase upload commands timeout from 1s to 8s. So i need to recompile bootloaders and upload it.
I was able to replace deprecated '-mshort-calls' to '-mrelax' and i'm able to compile it but it produces too large binary:
MBA-Anton:optiboot asmirnov$ make atmega328
../../../../tools/avr/bin/avr-gcc -g -Wall -Os -fno-inline-small-functions -fno-split-wide-types -mrelax -fno-move-loop-invariants -mmcu=atmega328p -DF_CPU=16000000L -mrelax '-DLED_START_FLASHES=3' '-DBAUD_RATE=115200' -c -o optiboot.o optiboot.c
../../../../tools/avr/bin/avr-gcc -g -Wall -Os -fno-inline-small-functions -fno-split-wide-types -mrelax -fno-move-loop-invariants -mmcu=atmega328p -DF_CPU=16000000L -mrelax '-DLED_START_FLASHES=3' '-DBAUD_RATE=115200' -Wl,--section-start=.text=0x7e00 -Wl,--section-start=.version=0x7ffe -Wl,--relax -Wl,--gc-sections -nostartfiles -nostdlib -o optiboot_atmega328.elf optiboot.o
../../../../tools/avr/bin/avr-size optiboot_atmega328.elf
text data bss dec hex filename
530 0 0 530 212 optiboot_atmega328.elf
../../../../tools/avr/bin/avr-objcopy -j .text -j .data -j .version --set-section-flags .version=alloc,load -O ihex optiboot_atmega328.elf optiboot_atmega328.hex
../../../../tools/avr/bin/avr-objdump -h -S optiboot_atmega328.elf > optiboot_atmega328.lst
rm optiboot.o optiboot_atmega328.elf
avr-gcc version:
MBA-Anton:optiboot asmirnov$ ../../../../tools/avr/bin/avr-gcc --version
avr-gcc (GCC) 4.8.1
Copyright (C) 2013 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
MBA-Anton:optiboot asmirnov$
I've googled more and i found that pretty the same config produces much smaller binary, so what's wrong?

Solved by using pretty old (2012 year) avr-gcc from here

Related

Size of the file produced by linker is too large

I have those three files that don't have many bytes.
.rw-r--r-- 936 28 Jan 00:53 kernel.o
.rw-r--r-- 512 28 Jan 00:53 kernel_entry.o
.rw-r--r-- 872 28 Jan 00:53 screen.o
I assembled kernel_entry.o using NASM 2.15.05:
nasm src/kernel_entry.asm -f elf32 -o ../build/obj/kernel_entry.o
And i compiled kernel.o and screen.o using gcc 11.1.0:
gcc -ffreestanding -m32 -fno-pie -c -Iinclude -MMD -o ../build/obj/kernel.o src/kernel.c
gcc -ffreestanding -m32 -fno-pie -c -Iinclude -MMD -o ../build/obj/driver/screen.o src/driver/screen.c
My goal is to produce 32 bit executable code.
Then i link those 3 files together using ld 2.36.31:
ld -m elf_i386 -Ttext 0x1000 --oformat binary ../build/obj/kernel_entry.o ../build/obj/kernel.o ../build/obj/driver/screen.o -o ../build/kernel.bin
However, I expected the output file to be the size of the three .o files but it is 135M which is way too much:
.rwxr-xr-x 135M 28 Jan 01:16 build/kernel.bin
Looking at the hexadecimal representation of the file, I see a little room for my instructions and the rest is just zeros.
Do you have any idea what's wrong? By removing the oformat parameter I get a decent size but the code does not execute as expected
Thank you.

How to link 2 files with different starting addresses with ld

I am trying to build a small os. I have an asm file that puts the processor in 64 bit mode with paging enabled. After this, i am jumping to my C code. I want the C code and asm code to be linked into the same file but the C code to have base address at 0xFFFFFF8000000000 and the asm file at 0x5000. How can I do this with ld.
This is what I have so far:
nasm -f elf64 os_init.asm -o ../bin/os_init.o
gcc -c -Os -nostdlib -nostartfiles -nodefaultlibs -fno-builtin vga/*.c utils/*.c *.c memory_management/*.c
ld -Ttext 0x5000 ../bin/os_init.o *.o -o ../bin/kernel.out
objcopy -S -O binary ../bin/kernel.out ../bin/kernel.bin
Currently both files are linked at 0x5000

gcc 4.8.2 cc1plus: error: unrecognized command line option "-std=gnu++11"

I have a system in which I've been compiling programs with C++11 using automake and everything has been working.
However, when a different user take the same exact Makefile they get the aforementioned error.
Now, if they type in g++ --std=gnu++11 program.cpp then there are no complaints.
I've narrowed it down to a line that is causing the issue; however, I don't know how to fix it.
depbase=`echo no_endl.o | sed 's|[^/]*$|.deps/&|;s|\.o$||'`;\
g++ -DHAVE_CONFIG_H -I. -I../.. -g --std=gnu++11 -g -O2 -MT no_endl.o -MD -MP -MF $depbase.Tpo -c -o no_endl.o no_endl.cpp &&\
mv -f $depbase.Tpo $depbase.Po
The problem is with depbase; however, I don't know what is going on with it or why it is there.
Below is the Automake file that I used to generate the Makefile:
bin_PROGRAMS = no_endl
AM_CXXFLAGS = -g --std=gnu++11
no_endl_SOURCES = no_endl.cpp
The only difference between my g++ and their g++ is I have an alias so g++ turns into g++ -g.
Helpful information
g++ --version
g++ (GCC) 4.8.2 20140120 (Red Hat 4.8.2-15)
Copyright (C) 2013 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
If there is any other information that I need to provide, then let me know.
Related Question
I solved the problem.
The problem was the $PATH variables for me and other users.
Now, other users have an alias for g++ to the newer version, but their $PATH omits the actual location.
Make works based on their $PATH variable, so to fix it, I just had to make sure their path had the same location as mine.
You probably need the following additional m4 macro file that handles corner cases of substituted C++11 compilers.
https://www.gnu.org/software/autoconf-archive/ax_cxx_compile_stdcxx_11.html#ax_cxx_compile_stdcxx_11

How to get gcc LTO work with library archives?

gcc experts,
I'm trying to use gcc lto with library archives, as the gcc comes with my system (RedHat Enterprise Linux 5.7) doesn't work with -flto (neither for my Ubuntu 14.10), so I build binutils && gcc from scratch.
Here is what I did:
1. Build binutils-2.22 with --enable-plugins
2. Build gcc-4.7.2 with --with-plugin-ld=/path/to/ld/built/in/step1 --enable-lto
3. Then for the following simple test:
// 1.c:
int foo(void)
{ return 0; }
// 2.c:
extern int foo(void)
int main(void)
{ return foo(); }
The following can get foo() inlined:
my_gcc -O3 -flto -c -o 1.o 1.c
my_gcc -O3 -flto -c -o 2.o 2.c
my_gcc -O3 -flto -o a.out 1.o 2.o
While the following can't:
my_gcc -O3 -flto -c -o 1.o 1.c
my_gcc -O3 -flto -c -o 2.o 2.c
my_ar cr --plugin <my_gcc>/libexec/gcc/x86_64-redhat-linux/4.7.2/liblto_plugin.so 1.a 1.o
my_ar cr --plugin <my_gcc>/libexec/gcc/x86_64-redhat-linux/4.7.2/liblto_plugin.so 2.a 2.o
gcc -O3 -flto -fuse-linker-plugin -o a.out 1.a 2.a
As the building system for the product I'm working on has to use archives, then what I can do to let lto work with library archive?
Your help will be much much appreciated.
Thanks a lot.
When linking, the order in which the libraries are listed on the command line, matters. So when compiling from the archives, you should swap 1.a and 2.a:
gcc -O3 -flto -fuse-linker-plugin -o a.out 2.a 1.a
I tested with gcc 4.9.2 and the disassembly, obtained with objdump -d a.out, shows that foo() is being inlined.

Why there is no debug info loaded after I add -g parameter

I think I have added the -g option in the makefile. The makefile is like this:
C=edgelist.c geometry.c heap.c main.c memory.c output.c voronoi.c
O=edgelist.o geometry.o heap.o main.o memory.o output.o voronoi.o
tt: voronoi t
./voronoi -t <t >tt
voronoi: $O
cc -g -o voronoi $O -lm
$O:vdefs.h
voronoi.tar : $C vdefs.h Makefile Doc t
tar -cf voronoi.tar $C vdefs.h Makefile Doc t
mailable: $C vdefs.h Makefile t
bundle $C vdefs.h Makefile t > mailable
When gdb runs the program:
jack#ubuntu:~/下载/voronoi$ gdb ./voronoi
GNU gdb (Ubuntu/Linaro 7.4-2012.04-0ubuntu2) 7.4-2012.04
Copyright (C) 2012 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "i686-linux-gnu".
For bug reporting instructions, please see:
<http://bugs.launchpad.net/gdb-linaro/>...
Reading symbols from /home/jack/下载/voronoi/voronoi...(**no debugging symbols found**)...done.
(gdb)
What's the problem?
Each of the object files has to be created with -g, and the executable has to be linked with -g. At the moment, you are linking with -g but not compiling with -g.
Given the structure of your makefile, the simplest fix is probably:
$O: vdefs.h
gcc -g -c $*.c
However, ideally, you should set things up so that you use CC and CFLAGS. For example:
CFLAGS = -g -Wall -Wextra -Werror
CC = gcc # May be unnecessary
LDLIBS = -lm
Then you don't need the gcc line after $O: vdefs.h. Your linking line should become:
${CC} -o $# ${CFLAGS} $O ${LDFLAGS} ${LDLIBS}
or thereabouts.

Resources