manually assembling and linking gcc ouput source files

I have gcc creating asm source files like this.

m68hc11-gcc -g -Wall -Werror -Winline -O -m68hcs12 -mshort -ffunction-sections -fomit-frame-pointer -msoft-reg-count=8 -mauto-incdec -fsigned-char -x cpp-output -S -o assembly/main.s preprocessedc/main.pp.c

from that output I can manually assemble it like this

m68hc11-as main.s -o main.o -m68hcs12

 or like this

m68hc11-gcc -g -Wall -Werror -Winline -O -m68hcs12 -mshort -ffunction-sections -fomit-frame-pointer -msoft-reg-count=8 -mauto-incdec -fsigned-char -c -o objects/freeEMS.o assembly/freeEMS.s
and it wont give me any errors

but when I try to link, I get this.
m68hc12-ld -defsym vectors_addr=0xF710 -m m68hc12elfb -T hc9s12xdp512elfb.x -o output/freeems-0.1.1-SNAPSHOT-Simple.elf objects/Simple.o objects/freeEMS.o objects/staticInit.o objects/main.o objects/tableLookup.o objects/init.o objects/utils.o objects/globalConstants.o objects/coreVarsGenerator.o objects/derivedVarsGenerator.o objects/fuelAndIgnitionCalcs.o objects/flashWrite.o objects/commsCore.o objects/blockDetailsLookup.o objects/interrupts.o objects/injectionISRs.o objects/ignitionISRs.o objects/commsISRs.o objects/realtimeISRs.o objects/miscISRs.o objects/FixedConfig1.o objects/FixedConfig2.o objects/IATTransferTable.o objects/CHTTransferTable.o objects/MAFTransferTable.o objects/TestTransferTable.o objects/FuelTables.o objects/FuelTables2.o objects/TimingTables.o objects/TimingTables2.o objects/TunableConfig.o objects/TunableConfig2.o objects/flashBurn.o
/usr/bin/m68hc11-ld: warning: cannot find entry symbol _start; defaulting to 0000c000
objects/Simple.o: In function `PrimaryRPMISR':
/home/fred/workspaces/home/freeems-vanilla/src/Simple.c:69: undefined reference to `_.tmp'
/home/fred/workspaces/home/freeems-vanilla/src/Simple.c:69: undefined reference to `_.z'
/home/fred/workspaces/home/freeems-vanilla/src/Simple.c:69: undefined reference to `_.xy'
/home/fred/workspaces/home/freeems-vanilla/src/Simple.c:69: undefined reference to `_.d1'
/home/fred/workspaces/home/freeems-vanilla/src/Simple.c:69: undefined reference to `_.d2'
/home/fred/workspaces/home/freeems-vanilla/src/Simple.c:69: undefined reference to `_.d3'
/home/fred/workspaces/home/freeems-vanilla/src/Simple.c:69: undefined reference to `_.d4'
/home/fred/workspaces/home/freeems-vanilla/src/Simple.c:69: undefined reference to `_.d5'
/home/fred/workspaces/home/freeems-vanilla/src/Simple.c:69: undefined reference to `_.d6'
and the list goes on

I can link from gcc, but I need to use an external linker that has special patches.

I'm not quite sure where I am going wrong.  Any input would be appreciated.
sean-keysAsked:
Who is Participating?
 
nociSoftware EngineerCommented:
I don't have specific knowledge of your platform (m86)..
But most of the time you also need some standard libraries like libc etc.
(you can specify them with -lc)

You might need more depending on the include files used.
0
 
Duncan RoeSoftware DeveloperCommented:
Please post line 69 of Simple.c.
As well as the libraries noci mentioned, if you are using ld instead of gcc then you will need to give it items like crtbegin.o and crtend.o or variants as typically found in /usr/lib/{gcc architecture}/{gcc revision}
Really I think you are better off to use gcc to to the loading - if you have loader options then you can pass them in with -Wl,{comma-separated option list} or multiple instances of -Wl,{ld option or option value}. I must confess I have only ever used the latter, although the documentation suggests to me that the former should work
0
 
Duncan RoeSoftware DeveloperCommented:
The -v flag to gcc used as a loader will show the ld command that is actually used
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

 
sean-keysAuthor Commented:
Ah yeah seems to work with -l /mygcclibs/....  

except  this except.....

mc9s12x-elf-ld: /usr/lib/gcc-lib/m68hc11/3.3.6-m68hc1x-20060122/m68hc12/mshort/crt1.o: Relocations in generic ELF (EM: 53)
/usr/lib/gcc-lib/m68hc11/3.3.6-m68hc1x-20060122/m68hc12/mshort/crt1.o: could not read symbols: File in wrong format
make: *** [output/freeems-0.1.1-SNAPSHOT-Simple.elf] Error 1
seank@mob-sean:/work/workspaceCDT/binutils-mc9s12x/build-testing/testing/f-mc9/freeems-vanilla/src$

So I need to recompile gcc using my binutils package correct?
0
 
Duncan RoeSoftware DeveloperCommented:
Wasn't m68hc11-gcc built that way already? Is the output you posted from a run of m68hc11-gcc?
I wonder if there is another m68hc11 crt1.o somewhere that you should load manually - locate crt1.o|grep -w crt1 will show
0
 
NovaDenizenCommented:
I don't have your m68hc11-gcc handy, so I'll use my gcc as an example of how to do this.

Say I have a file helloworld.c.  Normally I would just type "gcc -o helloworld helloworld.c" and everything is taken care of.  But I want to see the individual steps, so I use the "-v" flag too.
gcc -v -o helloworld helloworld.c

This produces lots of output, but you want to look for the compiler command, assembly command, and the link command.
The compiler command will look like this:
/path/to/cc <flags> helloworld.c <more flags> -o /var/tmp/blahblah.s
The assembly command will look something like this:
/path/to/as <flags> -o /var/tmp/blahblah.o /var/tmp/blahblah.s
The link command will look something like this:
/path/to/linker <flags> -o helloworld /path/to/crt1.o /path/to/crti.o <more crtXXX.o files here> /var/tmp/blahblah.o  <-L flags> <-l flags> /path/to/crtend.o /path/to/crtn.o

The compiler adds in those "crtxyz.o" files and -l flags to every c executable.  These are the C Run Time objects.  They wrap around your C program and are where the OS _really_ starts your program.  Some of these objects and libraries contain the symbols your program is missing.
0
 
sean-keysAuthor Commented:
That's just it crt1.o needs to be built with my new binutils. Which I have done, but now I get this error when I try to link more than one source file.

mc9$ make all
mc9s12x-elf-as --gstabs  -o main.o main.s
mc9s12x-elf-as --gstabs  -o xgate.o xgate.s
mc9s12x-elf-ld -m mc9s12xelfb --defsym _start=0 -o test.elf main.o xgate.o

 in bfd_default_compatible about to compare 31 and 31
 in bfd_default_compatible about to compare 31 and 31
mc9s12x-elf-ld: failed to merge target specific data of file xgate.o
 in bfd_default_compatible about to compare 31 and 31
 input was 31 output was 31make: *** [test.elf] Error 1

My printf code says they are of the same target, so I dont see what the problem is.
0
 
NovaDenizenCommented:
Try using the --no-warn-mismatch flag.  
0
 
sean-keysAuthor Commented:
That did work for linking two files generated with asm.  But when I try the gcc example I get

 m68hc11-gcc -mc9s12x -mshort -Wl,-m,mc9s12xelfb --no-warn-mismatch -Wl,-defsym,_io_ports=0x1000 -o test.elf main.o vectors.o
/usr/bin/mc9s12x-elf-ld: failed to merge target specific data of file main.o
/usr/bin/mc9s12x-elf-ld: failed to merge target specific data of file vectors.o
/usr/bin/mc9s12x-elf-ld: failed to merge target specific data of file /usr/lib/gcc-lib/m68hc11/3.3.6-m68hc1x-20060122/mshort/libgcc.a(_regs_min.o)
/usr/bin/mc9s12x-elf-ld: failed to merge target specific data of file /usr/lib/gcc-lib/m68hc11/3.3.6-m68hc1x-20060122/mshort/libgcc.a(_premain.o)
/usr/bin/mc9s12x-elf-ld: failed to merge target specific data of file /usr/lib/gcc-lib/m68hc11/3.3.6-m68hc1x-20060122/mshort/libgcc.a(__exit.o)
/usr/bin/mc9s12x-elf-ld: failed to merge target specific data of file /usr/lib/gcc-lib/m68hc11/3.3.6-m68hc1x-20060122/mshort/libgcc.a(_map_data.o)
/usr/bin/mc9s12x-elf-ld: failed to merge target specific data of file /usr/lib/gcc-lib/m68hc11/3.3.6-m68hc1x-20060122/mshort/libgcc.a(_init_bss.o)

 in bfd_default_compatible about to compare 31 and 31
 in bfd_default_compatible about to compare 31 and 31
 in bfd_default_compatible about to compare 31 and 31
 in bfd_default_compatible about to compare 31 and 31
 in bfd_default_compatible about to compare 31 and 31
 input was 31 output was 31
 in bfd_default_compatible about to compare 31 and 31
 input was 31 output was 31
 in bfd_default_compatible about to compare 31 and 31
 input was 31 output was 31
 in bfd_default_compatible about to compare 31 and 31
 input was 31 output was 31
 in bfd_default_compatible about to compare 31 and 31
 input was 31 output was 31
 in bfd_default_compatible about to compare 31 and 31
 input was 31 output was 31
 in bfd_default_compatible about to compare 31 and 31
 input was 31 output was 31collect2: ld returned 1 exit status
0
 
NovaDenizenCommented:
You need to tell gcc that the --no-warn-mismatch is for the linker.  You do that by using "-Xlinker --no-warn-mismatch"
0
 
sean-keysAuthor Commented:
That did allow my sample project to build.  But my main project which requires newlib fails.  So i figured I could just recompile newlib.  It builds most of it except the last section.

Configured with: ../src/configure --prefix=/usr --infodir=/usr/share/info --mandir=/usr/share/man --with-gnu-as --with-gnu-ld --enable-nls --without-inclu$
Thread model: single
gcc version 3.3.6-m68hc1x-20060122
 /usr/lib/gcc-lib/m68hc11/3.3.6-m68hc1x-20060122/collect2 -m m68hc11elf -relax /usr/lib/gcc-lib/m68hc11/3.3.6-m68hc1x-20060122/crt1.o -L/usr/lib/gcc-lib/m$
/usr/lib/gcc-lib/m68hc11/3.3.6-m68hc1x-20060122/crt1.o:(.install4+0x1): undefined reference to `main'

I wonder if I my "mc9s12x-elf-ld: failed to merge target specific data of file" problem is really a problem.,,,,
0
 
nociSoftware EngineerCommented:
A shared library has no function main(). ==> you need a different start module from crt1.o
(that's for a main program)
The main() function is reserved for the entry point of any program, so it cannot be in a library.

Check out what gcc uses on you platform for linking a shared object. for a shared object you add
 "-shared"  to your gcc command line.
0
 
NovaDenizenCommented:
What noci said.  Figure out how to compile and link a library using a stock compiler, do the same test with your compiler to make sure you have it down, then use -v to see the flags and other hidden stuff.
0
 
sean-keysAuthor Commented:
Thanks guys, I was able to use the information in this thread to get it working!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.