Resolving “branch out of range” Errors When Using Older MIPS Compilers
In embedded software design it is common to want to have the code footprint as small as possible. Therefore, it is fairly common when using the GNU C++ compiler to use the -O2 option which includes the -O optimization flags plus a whole slew of others. You would think that the best optimization is always what you want when compiling embedded systems code. Not so, my young apprentice, at least that’s what I found out the hard way.
I was building some code using a compiler created for MIPS and I began getting a long list of messages from standard input that looked like this:
Warning: AT used after ".set noat" or macro used after ".set nomacro"
These were followed by a long list errors that looked like this:
Error: Branch out of range
Needless to say, I wasn’t sure what was going on. Well after a little bit of research a senior developer pointed out that the code had been assembled in such a way that a jump was being attempted that was more than 32K instructions away, thus causing the error. And how did we fix this problem? By removing the -O2 option from the compilation command.
You wouldn’t normally run into these kinds of issues with modern compilers but it turns out we were forced to use an older compiler (3.3.1) with an older version of the standard C++ library (2.2.5). So there you have it. A small tip when using older compilers.

August 8th, 2009 at 12:13 pm
hi,
i am getting Branch out of range error when i compile the wireless driver madwifi trunk with mips processor. am using compex wp543 with openwrt kamikaze 8.09 linux-2.6.26.5 kernel, gcc 4.1.2
but when i try to use -O0 the branchout of reach error occurance is getting more. can you help me how can i solve this
December 19th, 2009 at 3:38 pm
Try these options:
-G 0 -mips32 -mno-abicalls -fno-pic -Wa,–trap
-Wa,-relax-branch -fno-strict-aliasing -fno-common -fomit-frame-pointer
-mlong-calls