* nasm / code distance @ 2006-04-07 18:47 Thiago Silva 2006-04-08 9:47 ` leslie.polzer 2006-04-08 14:55 ` Frank Kotler 0 siblings, 2 replies; 4+ messages in thread From: Thiago Silva @ 2006-04-07 18:47 UTC (permalink / raw) To: linux-assembly Hello, I'm having some troubles with an asm application. The first problem is about the "short jump". I get those messages "error: short jump is out of range" when using nasm on my sources. Researching, I found that people answer this by saying "use -O1". Now, is this the appropriated way to deal with this problem? Second, I've been getting some strange (?) segfaults... Using valgrind, I found that there were 3 invalid reads on memory. Those reads uses data in the .data section. Now, moving the problematic function to the beginning of the file, close to the entry point and other sections, valgrind didn't complain and I got no segfaults. I'm obviously missing something, so...can anyone help? Thanks in advance, -- +Thiago Silva ^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: nasm / code distance 2006-04-07 18:47 nasm / code distance Thiago Silva @ 2006-04-08 9:47 ` leslie.polzer 2006-04-08 11:09 ` Thiago Silva 2006-04-08 14:55 ` Frank Kotler 1 sibling, 1 reply; 4+ messages in thread From: leslie.polzer @ 2006-04-08 9:47 UTC (permalink / raw) To: Thiago Silva; +Cc: linux-assembly [-- Attachment #1: Type: text/plain, Size: 326 bytes --] On Fri, Apr 07, 2006 at 06:47:32PM +0000, Thiago Silva wrote: > Hello, I'm having some troubles with an asm application. It's hard to say anything specific without you showing the problematic parts of your code. Leslie -- gpg --keyserver pgp.mit.edu --recv-keys 0x52D70289 http://nic-nac-project.de/~skypher/ [-- Attachment #2: Type: application/pgp-signature, Size: 191 bytes --] ^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: nasm / code distance 2006-04-08 9:47 ` leslie.polzer @ 2006-04-08 11:09 ` Thiago Silva 0 siblings, 0 replies; 4+ messages in thread From: Thiago Silva @ 2006-04-08 11:09 UTC (permalink / raw) To: linux-assembly Well...the code is really a big one. To clarify, I made simple test: On the entry point, I read some data in the .data section (such as "mov eax, dword [_r]"). Running valgrind, this read is not shown on the error reporting. Then, I copied this same read code and pasted it on a function in the end of the file (is a pretty big source file). Then, valgrind complained that this was an invalid read!! So I though there might be something I'm missing... The same goes for the jmp instructions getting the "short jump out of range". On Saturday 08 April 2006 09:47, you wrote: > On Fri, Apr 07, 2006 at 06:47:32PM +0000, Thiago Silva wrote: > > Hello, I'm having some troubles with an asm application. > > It's hard to say anything specific without you showing the problematic > parts of your code. > > Leslie -- +Thiago Silva ^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: nasm / code distance 2006-04-07 18:47 nasm / code distance Thiago Silva 2006-04-08 9:47 ` leslie.polzer @ 2006-04-08 14:55 ` Frank Kotler 1 sibling, 0 replies; 4+ messages in thread From: Frank Kotler @ 2006-04-08 14:55 UTC (permalink / raw) To: Thiago Silva; +Cc: linux-assembly Thiago Silva wrote: > Hello, > I'm having some troubles with an asm application. > > The first problem is about the "short jump". > I get those messages "error: short jump is out of range" when using nasm on my > sources. Researching, I found that people answer this by saying "use -O1". > Now, is this the appropriated way to deal with this problem? That's one way. Jumps, conditional or unconditional come in two sizes, "short" and "near". Weird that the longer jump is called "near", but that's the way it is. There's also a "far" jump which loads cs as well as eip. This isn't very useful in Linux, at least not in "user" code... Nasm defaults to "near" for unconditional jumps: jmp my_label Will jump +/- 2G-1 bytes (assuming 32-bit code). This will always "reach", but may produce longer code than necessary, since the parameter takes up four bytes. If the target is within -128/+127 bytes, we can do: jmp short my_label ...and the parameter is stored as just one byte. (note that these instructions use "relative addressing" - we write "my_label", but what's emitted for code is the "distance", forward or back, to "my_label") If you do this, and "my_label" is more than 127 bytes away - or becomes so as code is modified - you'll see that "out of range" message. [H.P. Recktenwald (Hi, hp!) showed me a neat trick - pre "-O" switch - to deal with this - "%define jm jmp short", then use "jm" for all your unconditional jumps. If Nasm complains "out of range", just add "p"!] Conditional jumps default to "short". Nasm doesn't want to default to "near", since it's not available on 8086 (and we write a lot of code for 8086 :)... If the target is farther than 127 bytes away, we can: jz near my_label The "-O" switch alters these defaults. "-O1" is "special" - it will size a "backward" jump "as required", but forward jumps are "near", to guarantee they'll reach. Parameters greater than 1 enable that many (maximum) passes, so that jumps can be "sized" optimally. It *used* to be that "-O2" and "-O3" were "special cased" to 10 and 15 passes, but this has been changed - "-O2" and "-O3" are *not* enough, and are in fact "buggy" currently. There is no advantage to using a small number - Nasm quits when it's done. I usually use "-O999" if I use it at all. The other effect of the "-O" switch - any parameter (>0) - is to enable the shorter "signed byte" form of instructions that have a short form, but that's a different issue... > Second, I've been getting some strange (?) segfaults... > Using valgrind, I found that there were 3 invalid reads on memory. > Those reads uses data in the .data section. Now, moving the problematic > function to the beginning of the file, close to the entry point and other > sections, valgrind didn't complain and I got no segfaults. > > I'm obviously missing something, so...can anyone help? As Leslie says, I think we're going to need to see the code for that one. Doesn't sound right. (possibly from using "-O2"??? - does ld complain about "can't find entrypoint"???) Best, Frank ^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2006-04-08 14:55 UTC | newest] Thread overview: 4+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2006-04-07 18:47 nasm / code distance Thiago Silva 2006-04-08 9:47 ` leslie.polzer 2006-04-08 11:09 ` Thiago Silva 2006-04-08 14:55 ` Frank Kotler
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox; as well as URLs for NNTP newsgroup(s).