Nios® V/II Embedded Design Suite (EDS)
Support for Embedded Development Tools, Processors (SoCs and Nios® V/II processor), Embedded Development Suites (EDSs), Boot and Configuration, Operating Systems, C and C++
Announcements
FPGA community forums and blogs on community.intel.com are migrating to the new Altera Community and are read-only. For urgent support needs during this transition, please visit the FPGA Design Resources page or contact an Altera Authorized Distributor.
12748 Discussions

Linux development environment

Altera_Forum
Honored Contributor II
3,014 Views

Does anyone know if the Nios II port of uClinux is available as a simple tarball (i.e. tar.gz or .tgz) that can be downloaded to a Linux machine and configured and built using the nios2-elf-gcc cross compiler? 

 

I am actively attempting to rebuild the nios2-gnutools on my Linux box so I can develop the kernel and user applications on it. I will post the results if I am successful. However, if someone has already accomplished this I would be very interested in getting the source.
0 Kudos
26 Replies
Altera_Forum
Honored Contributor II
1,209 Views

Just use the source supplied with the Windows "uClinux 2.6 Distribution for Nios II" posted in the forum. 

Remove CRs from the source-files by using dos2unix or stripcr.c included at the end of this mail (have also included a script which calls this prog.). 

 

Build instructions: 

 

Installdir=/usr/local/nios2-gnutools (or whatever you want) 

Sourcedir contents: 

binutils 

gdb 

gcc 

make 

newlib 

 

Get rid of Cygwin inserted CRs: 

 

./stripcr.sh 

 

In sourcedir do: 

 

[1] Binutils 

 

mkdir binutils-build 

cd binutils-build 

../binutils/configure --target=nios2-elf --program-prefix=nios2-elf- --prefix=$Installdir --exec_prefix=$Installdir -v 

make all 

make install 

 

[2] GCC 

export PATH=$PATH:$Installdir 

mkdir gcc-build 

cd gcc-build 

../gcc/configure --target=nios2-elf --program-prefix=nios2-elf- --prefix=$Installdir --exec_prefix=$Installdir --with-newlib --with-headers=../newlib/newlib/libc/include -v 

make all 

make install 

 

[3] Make 

mkdir make-build 

cd make-build 

../make/configure --target=nios2-elf --program-prefix=nios2-elf- --prefix=$Installdir --exec_prefix=$Installdir -v 

make all 

make install 

 

[4] Newlib 

cp newlib/newlib/configure.host.normalc newlib/newlib/configure.host 

mkdir newlib-build 

cd newlib-build 

../make/configure --target=nios2-elf --program-prefix=nios2-elf- --prefix=$Installdir --exec_prefix=$Installdir -v 

make all 

make install 

 

[5] GDB 

mkdir gdb-build 

cd gdb-build 

../make/configure --target=nios2-elf --program-prefix=nios2-elf- --prefix=$Installdir --exec_prefix=$Installdir -v 

make all 

make install 

 

Hope this is helpful 

 

Regards 

Atle 

 

---------------------------------------------- 

stripcr: 

----------------------------------------------# include <stdio.h># include <string.h># include <errno.h> 

 

char strip_tmpfile[]="/tmp/stripCR.tmp"; 

int custom_copy (const char* in, const char* out, int stripCR); 

 

int 

main (argc, argv) 

int argc; 

char* argv[]; 

int result; 

while (--argc > 0) { 

argv++; 

result = custom_copy (argv[0], strip_tmpfile, 1); 

if (result==1) { 

if (custom_copy (strip_tmpfile, argv[0], 0)) exit(-1); 

printf(" Stripping CR from file %s\n", argv[0]); 

}else { 

if (result!=0) exit(-1); 

return 0; 

 

 

/* return value:  

0: no strip, 1: file stripped, otherwise error */ 

int 

custom_copy (const char* in, const char* out, int stripCR) { 

FILE *ifp; /* input file pointer */ 

FILE *ofp; /* output file pointer */ 

char ch; 

int result=0; 

 

ifp = fopen (in, "rb"); 

ofp = fopen (out, "wb"); 

 

if (ifp == NULL) { 

printf ("Could not open %s for reading\n", in); 

return 0; 

 

if (ofp == NULL) { 

printf ("Could not open %s for writing.\n", out); 

return -1; 

 

while (!feof (ifp)) { 

if(fread ((void*) &ch, 1, 1, ifp)) { 

if (stripCR && (ch == (char) 0x0D)) { 

result=1; 

continue; 

fwrite((void*) &ch, 1, 1, ofp); 

 

fclose (ifp); 

fclose (ofp); 

return result; 

 

------------------------------------------------------- 

stripcr.sh

-------------------------------------------------------# !/bin/bash# Strips CRs from all Makefiles and sourcefiles 

# stripcr binary 

STRIPCR=$PWD"/stripcr/stripcr" 

SRCDIR=$PWD"/src/" 

 

echo $PWD 

# Makefiles 

find $SRCDIR \( -name &#39;Makefile&#39; -o -name &#39;Makefile.in&#39; \) -exec $STRIPCR {} \; 

# Source files 

find $SRCDIR \( -name &#39;*.c&#39; -o -name &#39;*.h&#39; -o -name &#39;*.S&#39; \) -exec $STRIPCR {} \; 

# Linker scripts 

find $SRCDIR \( -name &#39;*.ld&#39; \) -exec $STRIPCR {} \; 

# Awk, sed and perl scripts 

find $SRCDIR \( -name &#39;*.awk&#39; -o -name &#39;*.sed&#39; -o -name &#39;*.pl&#39; \) -exec $STRIPCR {} \; 

# Config 

find $SRCDIR \( -name &#39;config.in&#39; -o -name &#39;configure.in&#39; -o -name &#39;configure&#39; \) -exec $STRIPCR {} \; 

# Other 

find $SRCDIR \( -name &#39;*.md&#39; \) -exec $STRIPCR {} \;
0 Kudos
Altera_Forum
Honored Contributor II
1,209 Views

Ooops. Sorry.. the tools are of course included with the Altera Nios kit.

0 Kudos
Altera_Forum
Honored Contributor II
1,209 Views

I am having some level of success following your instructions. 

 

Your stripcr program and script has been very helpful. I did find that I had to change it so that instead of Makefile.in it looks in any Makefile.* and instead of config.in, it looks at any config.*. I also found that throughout the build I would have to run the script again to strip newly generated files of their carriage returns. 

 

Currently I am hung up on the newlib configuration/compile. It is having problems with the following directory of the install: 

nios2-gnutools/src/newlib-build/nios2-elf/mno-hw-mul/newlib/libc/sys/. 

The Makefile contains no definition for "sys_dir". Apparently this is where the specific system is called out. 

In looking at the nios2-gnutools/src/newlib/newlib/libc/sys directory, there are several systems listed. None of them are related to nios, but there is one for linux. I have tried to get the Makefile to use this directory, but it still has issues. 

 

Were you able to get your newlib/ items to build properly without any tweaking? If so, was your "sys_dir" variable defined? It was not defined in my setup. 

 

Thanks again for your help.
0 Kudos
Altera_Forum
Honored Contributor II
1,209 Views

I have just tried to build the tools, and have not experienced any problems (newlib builds just fine). The only thing I&#39;ve done different from the HOWTO I posted was to build only C and C++ for gcc by using: --enable-languages=c,c++. As you say you have to run stripcr() on autogenerated files, maybe that CRs is your problem. Here is an updated version of the stripcr.sh script: 

# !/bin/bash# Strips CRs (Cygwin ......) from all Makefiles and sourcefiles 

# stripcr binary 

STRIPCR="/usr/local/bin/stripcr" 

SRCDIR=$PWD 

 

echo "Stripping CR in "$SRCDIR 

# Config 

find $SRCDIR \( -name &#39;config.*&#39; -o -name &#39;configure.*&#39; -o -name &#39;configure&#39; \) -exec $STRIPCR {} \; 

# Scripts + templates 

find $SRCDIR \( -name &#39;*.sc&#39; -o -name &#39;*.sh&#39; \) -exec $STRIPCR {} \; 

# Makefiles 

find $SRCDIR \( -name &#39;Makefile&#39; -o -name &#39;Makefile.*&#39; \) -exec $STRIPCR {} \; 

# Source files 

find $SRCDIR \( -name &#39;*.c&#39; -o -name &#39;*.h&#39; -o -name &#39;*.S&#39; -o -name &#39;*.s&#39; \) -exec $STRIPCR {} \; 

# Linker scripts 

find $SRCDIR \( -name &#39;*.ld&#39; \) -exec $STRIPCR {} \; 

# Awk, sed and perl scripts 

find $SRCDIR \( -name &#39;*.awk&#39; -o -name &#39;*.sed&#39; -o -name &#39;*.pl&#39; \) -exec $STRIPCR {} \; 

# Other 

find $SRCDIR \( -name &#39;*.md&#39; \) -exec $STRIPCR {} \;
0 Kudos
Altera_Forum
Honored Contributor II
1,209 Views

Thanks again for your help. I will untar a pristine tarball and try the process over. 

 

I have assumed that for the newlib portion in step 4, you meant to have "../newlib/configure" instead of "../make/configure". 

Is this correct, or is this why I&#39;m having problems?
0 Kudos
Altera_Forum
Honored Contributor II
1,209 Views

Correct, it was just a little typo. And in step 5 it should be ../gdb/configure

0 Kudos
Altera_Forum
Honored Contributor II
1,209 Views

Success! 

 

Thank you very much for your help. 

I followed your lead and used the c,c++ compiler options for newlib. I don&#39;t know if this fixed the problem. 

I tend to think that all along it was just more carriage returns getting in the way. 

Your new script worked great. 

 

I now have the tools all built on my Linux box. 

 

The next issue I&#39;m facing is getting compiled programs to run on my nios2 development board. 

A couple of the programs are: "cpuload" and "uart_send". 

 

I&#39;ve created a few small programs, but when I try to run them on the target I get the following errors: 

-------------------------------------# ./cpuload & 

BINFMT_FLAT: bad magic/rev (0x10101ff, need 0x4) 

BINFMT_FLAT: bad magic/rev (0x10101ff, need 0x4) 

27# ^ELF@@@^: not found 

# ./uart_send & 

BINFMT_FLAT: bad magic/rev (0x10101ff, need 0x4) 

BINFMT_FLAT: bad magic/rev (0x10101ff, need 0x4) 

29# ^ELF@@@^: not found 

------------------------------------ 

I couldn&#39;t copy some of the symbols verbatim. 

The ^ is actually a triange and the @ is actually a smiley face. Anyway, they are just binary. 

 

I am going to try again but this time compile them statically so I&#39;ll know if it is a compiler or a library problem.
0 Kudos
Altera_Forum
Honored Contributor II
1,209 Views

After compiling them statically, I just get a "cannot execute" error. 

 

I get this same error if I try to run the program on my development machine. So, it wasn&#39;t compiled natively and the nios2-elf-gcc cross-compiler didn&#39;t seem build it for the nios2 architecture either. Am I missing something here? 

 

Shouldn&#39;t I just be able to use "nios2-elf-gcc" to compile a program and have it run on the nios2? 

I can understand if there are library issues. Perhaps the libraries on my target don&#39;t match the libraries for my development environment. But, if I compile the programs statically, it should just come down to a compiler issue.
0 Kudos
Altera_Forum
Honored Contributor II
1,209 Views

<div class='quotetop'>QUOTE </div> 

--- Quote Start ---  

Shouldn&#39;t I just be able to use "nios2-elf-gcc" to compile a program and have it run on the nios2?[/b] 

--- Quote End ---  

 

not if you run the program on uClinux. Under uClinux, we are using a FLAT binary format, not ELF (which could not run on uClinux). You need a tool, elf2flt, to convert ELF format to FLAT format. Please check the userland Rules.mak for details. You also need the source code of elf2flt (with Nios II support) so that you can rebuilt it on Linux. 

 

Regards, 

wentao 

Microtronix.
0 Kudos
Altera_Forum
Honored Contributor II
1,209 Views

I want to know if it is built under RedHat9.  

 

Thanks.
0 Kudos
Altera_Forum
Honored Contributor II
1,209 Views

Yes, 

 

I am building all of these tools under Redhat 9.
0 Kudos
Altera_Forum
Honored Contributor II
1,209 Views

But where can download the nios2-gnutools. And if install successfully, which OS can we use for nios2.

0 Kudos
Altera_Forum
Honored Contributor II
1,209 Views

I have extracted the source code for the elf2flt utility that was part of the Nios II IDE installation. 

 

I found it in the altera/kits/nios2/examples/software/linux/utils directory. 

I moved this directory over to my Linux machine and ran dos2unix on all the files. 

I&#39;ve been trying to get it configured and build properly, but I&#39;m running into a problem with an 

 

"undefined reference to &#39;__getreent&#39; " error. 

 

The README for the elf2flt utility only discusses the inclusion of the libbfd.a and the libiberty.a libraries. 

I think I&#39;ve pointed to the correct location for these, but the __getreent symbol is still not found. 

 

The interesting thing is that non of the elf2flt c files actually call the __getreent function. The errors point to the fprintf function. 

It must be using the __getreent. 

 

Does anyone know how to resolve this error?
0 Kudos
Altera_Forum
Honored Contributor II
1,209 Views

I successfully compiled the elf2flt utility under Linux. 

 

Now I am having a problem using the elf2flt utility. I have the same problem under my Cygwin environment under Windows. 

 

If I use nios2-elf-gcc to compile a simple program (e.g. nios2-elf-gcc hello.c -o hello) and try to use the elf2flt utility to convert the format to flat, it gives me the following error: 

 

hello: Input file contains no relocation info 

 

The hello file is the executable that was created directly from the nios2-elf-gcc compiler. 

If I try to run the hello file directly on my uKit Nios II development board, then I get the format errors described a couple replies back. 

 

WHAT AM I MISSING HERE?
0 Kudos
Altera_Forum
Honored Contributor II
1,209 Views

hi trk_golf, 

 

<div class='quotetop'>QUOTE </div> 

--- Quote Start ---  

If I use nios2-elf-gcc to compile a simple program (e.g. nios2-elf-gcc hello.c -o hello) and try to use the elf2flt utility to convert the format to flat...[/b] 

--- Quote End ---  

 

 

If you want to run hello on uClinux, you have to compile it with uClibc, not the default newlib. Therefore, here you must explicitly specify the crt0.o and libc.a from uClibc. (we do this because the tool chain is not built against uclibc, but newlib). 

 

You also need to tell the linker to generate relocatable outputs, so that elf2flt can convert them. This is done by passing "-r -d" to the linker. (You could also try "-q" to do a final link and keep the relocation records, but it was broken when I tried about half year ago).  

 

For details, please refer to the file Rules.mak for building userland applications on Cygwin. 

 

Regards, 

wentao 

Microtronix
0 Kudos
Altera_Forum
Honored Contributor II
1,209 Views

Thanks for all your advice. I&#39;m moving forward, but I&#39;m still running into problems with converting the file to a flat binary. 

 

I have used the Rules.mak file as a reference. It is located at "altera/kits/nios2/bin/eclipse/plugins/com.microtronix.nios2linux.application_0.1.0/templates" on my Windows machine. 

I have also tried using the elf2flt.ld linker script located at "com.microtronix.nios2linux.application_0.1.0/scripts". 

 

No matter what I try, I keep getting an error from nios2-elf-elf2flt that states "cpuload.elf: Input file contains no relocation info". 

However, if I perform a "nios2-elf-objdump -f cpuload.elf", then I get the following results: 

 

cpuload.elf: file format elf32-littlenios2 

architecture: nios2, flags 0x00000011: 

HAS_RELOC, HAS_SYMS 

start address 0x00000000 

 

Doesn&#39;t the output from objdump indicate that the elf image does indeed have relocation information? 

Did I compile elf2flt improperly on my Linux machine? 

 

Any other suggestions?
0 Kudos
Altera_Forum
Honored Contributor II
1,209 Views

It sounds like the elf2flt is not built correctly. In the elf2flt src directory are there some libraries (libbfd.a and libiberty.a), which are from the building of the nios2 toolchain under Cygwin, you are supposed to replace them with those you built under Linux.  

I dont know if you have done this. 

 

wentao
0 Kudos
Altera_Forum
Honored Contributor II
1,209 Views

I&#39;m getting closer... 

 

I have rebuilt elf2flt, but I still get an error. 

 

Here are the steps I took to rebuild the elf2flt tool: 

 

I copied 

 

nios2-gnutools/src/binutils-build/bfd/bfd.h 

nios2-gnutools/src/binutils-build/bfd/libbfd.a 

nios2-gnutools/src/binutils-build/libiberty/libiberty.a 

 

into my elf2flt directory. 

 

I then ran the following configure command: 

 

./configure --target=nios2-elf --with-bfd-include-dir=. --with-libbfd=./libbfd.a --with-libiberty=./libiberty.a 

 

This configures without errors. 

 

I then run make. This succeeds. 

 

If I use the resulting elf2flt binary to try and convert the cpuload.elf file I created, it provides the following error? 

 

ERROR: text=0xaf0 overlaps data=0x0 ? 

 

Does this have something to do with the linker script? 

 

I apologize for my ignorance. I&#39;ve never dealt with an elf image and linkers that much until now. 

 

I am grateful for your help.
0 Kudos
Altera_Forum
Honored Contributor II
1,209 Views

Here is the output from nios2-elf-readelf -S cpuload.elf 

 

I see that the .text section has a size of 0x0af0 and is at offset 0x38. That&#39;s about all that I can relate to the error. 

It would appear that this fits in without overlapping anything. 

Any idea what the error might be related to? 

 

 

<div class='quotetop'>QUOTE </div> 

--- Quote Start ---  

There are 27 section headers, starting at offset 0x54d8: 

 

Section Headers: 

  [Nr] Name              Type            Addr  Off    Size ES Flg Lk Inf Al 

  [ 0]                NULL            00000000 000000 000000 00      0 0  0 

  [ 1] .text          PROGBITS        00000000 000038 000af0 00  AX  0 0  8 

  [ 2] .rela.text        RELA            00000000 005910 000318 0c  25 1  4 

  [ 3] .rodata        PROGBITS        00000000 000b28 000010 00 A  0 0  4 

  [ 4] .data          PROGBITS        00000000 000b38 000018 00  WA  0 0  4 

  [ 5] .sdata            PROGBITS        00000000 000b50 000018 00 WAp  0 0  4 

  [ 6] .sbss          NOBITS          00000000 000b68 000004 00 WAp  0 0  4 

  [ 7] .bss              NOBITS          00000000 000b68 000000 00  WA  0 0  1 

  [ 8] .comment          PROGBITS        00000000 000b68 000160 00      0 0  1 

  [ 9] .debug_aranges    PROGBITS        00000000 000cc8 0000c0 00      0 0  1 

  [10] .rela.debug_arang RELA            00000000 005c28 000090 0c  25 9  4 

  [11] .debug_pubnames PROGBITS        00000000 000d88 0000ee 00      0 0  1 

  [12] .rela.debug_pubna RELA            00000000 005cb8 000054 0c  25 b  4 

  [13] .debug_info    PROGBITS        00000000 000e76 0017ca 00      0 0  1 

  [14] .rela.debug_info  RELA            00000000 005d0c 001758 0c  25 d  4 

  [15] .debug_abbrev  PROGBITS        00000000 002640 000a9d 00      0 0  1 

  [16] .debug_line    PROGBITS        00000000 0030dd 000d15 00      0 0  1 

  [17] .rela.debug_line  RELA            00000000 007464 000cc0 0c  25  10  4 

  [18] .debug_frame      PROGBITS        00000000 003df4 0001ec 00      0 0  4 

  [19] .rela.debug_frame RELA            00000000 008124 0000c0 0c  25  12  4 

  [20] .debug_str        PROGBITS        00000000 003fe0 001322 01  MS  0 0  1 

  [21] .ctors            PROGBITS        00001324 005304 000014 00 WAX  0 0  4 

  [22] .dtors            PROGBITS        00001338 005318 000014 00 WAX  0 0  4 

  [23] .debug_ranges  PROGBITS        0000134c 00532c 0000c8 00      0 0  1 

  [24] .shstrtab      STRTAB          00000000 0053f4 0000e1 00      0 0  1 

  [25] .symtab        SYMTAB          00000000 0081e4 000860 10  26  39  4 

  [26] .strtab        STRTAB          00000000 008a44 0005e1 00      0 0  1 

Key to Flags: 

  W (write), A (alloc), X (execute), M (merge), S (strings) 

  I (info), L (link order), G (group), x (unknown) 

  O (extra OS processing required) o (OS specific), p (processor specific)[/b] 

--- Quote End ---  

 

0 Kudos
Altera_Forum
Honored Contributor II
1,159 Views

I&#39;ve been looking at the source code for the elf2flt utility. 

I also ran it in verbose mode. 

 

With the -v option, its output is the following: 

 

TEXT -> vma=0x0 len=0xaf0 

DATA -> vma=0x0 len=0x18 

ERROR: text=0xaf0 overlaps data=0x0 ? 

 

 

I would think that it would us the offset information, not the address to determine if something is overlapping. 

Isn&#39;t this the point in having relocatable code, it uses offsets instead of fixed addresses ? 

 

Perhaps I&#39;ve done something wrong or the linker script isn&#39;t correct. 

 

I&#39;ll keep digging around. If you have any suggestions, feel free to let me know.
0 Kudos
Reply