How to make the assembler/linker change the ordering of the sections ?

When assembling masm files the section follow this order

.text
.idata
.rdata
.data
.rsrc

But the result i need is

.text
.data
(...)
.rsrc

In other hands, the .data section must be imediattly after the text (.code) section.

The code i wrote has this steps.

.586p
.model flat, stdcall
option casemap :none
(includes)
.data
(data stuff)
.code
(coding...)

But the result put the data section at the end, insetad just after the text section.

I tried either

.586
OPTION CASEMAP:NONE,LANGUAGE:STDCALL,DOTNAME
(includes)

_TEXT SEGMENT READONLY PAGE PUBLIC USE32 'CODE'
start:
(coding...)

end start
_TEXT ENDS

_DATA SEGMENT PUBLIC USE32 'DATA'
(data stuff)
_DATA ENDS

ASSUME CS: FLAT, DS:FLAT, SS:FLAT, ES:FLAT

But there is an error message during assembling

D:\(...)\test.asm(2717) : fatal error A1010: unmatched blo
ck nesting : _TEXT

The code section is big (About 700 kb )

The name of the code section doesn't seems to matter (.text or .CODE), what is relevant is the data section must be after it.

What am i doing wrong ?


Best regards,

Beyond2000!
Posted on 2002-02-17 22:18:48 by Beyond2000!
Define your segments, and their ordering at the top of the assembly file. Then use the segments when ever you want later in the code. As soon as MASM hits the END statement your done! Here is an example of what I mean:


.586
OPTION CASEMAP:NONE,LANGUAGE:STDCALL,DOTNAME
(includes)

; first instance defines ordering...
_TEXT SEGMENT READONLY PAGE PUBLIC USE32 'CODE'
_TEXT ENDS

_DATA SEGMENT PUBLIC USE32 'DATA'
_DATA ENDS


ASSUME CS: FLAT, DS:FLAT, SS:FLAT, ES:FLAT


; parameters of segment defined above.
_DATA SEGMENT
(data stuff)
_DATA ENDS

; this uses the existing segment defined above
_TEXT SEGMENT

start:
(coding...)

_TEXT ENDS

end start
I'm not sure this will solve your problem though? :confused:
Posted on 2002-02-18 01:32:29 by bitRAKE
Tks,

but, it didn't worked yet

the result is still

.text
.idata
.rdata
.data
.rsrc

I tryed the .seq command, but the linker did not recognize many libraries (_getmodulehandle@, etc...)

I've got stucked on this stuff for hours....

I'll continue when i come back of he work.
Posted on 2002-02-18 02:34:29 by Beyond2000!
btw

this is the error message

Assembling: D:\masm32\novoteste\novo\adicional2a.asm
D:\masm32\test.asm(28) : error A2015: segment attributes c
annot change : Alignment


that is :

segment attributes cannot change : attribute

A segment was reopened with different attributes than it was opened with originally.

When a SEGMENT directive opens a previously defined segment, the newly opened segment inherits the attributes the segment was defined with.

the assembler specifications was the defaul on bldall.bat

\masm32\bin\ml /c /coff test.asm
if errorlevel 1 goto errasm

if not exist rsrc.obj goto nores

\masm32\bin\Link /SUBSYSTEM:WINDOWS test.obj rsrc.obj
Posted on 2002-02-18 02:48:03 by Beyond2000!
I believe you should have
"_TEXT ends" before "end start", as masm stops reading the input
file after it hits an "end".
Posted on 2002-02-18 02:49:45 by f0dder
Try to use programs like PEditor. Or manually :), or write your own program.
Posted on 2002-02-18 02:51:24 by masquer
A program like PEeditor wont help unless it has a built-in (re)linker.
Sure, you can change the order the raw section data is in the PE
file, but you cannot move the sections around (for obvious reasons).

Also, I believe that it is link.exe that sets up the section order, and
it doesn't care too much what you want. I had to use fasm to get
the section ordering I needed.
Posted on 2002-02-18 02:57:47 by f0dder
Why not in PEditor :)
1. Open file in it.
2. Press section button
3. Right click on section you want to move (and keep in mind virtual offset)
4. Choose move to HDD. Save the section on HDD
5. Choose Copy section from HDD to EOF.
6. Right click on inserted section and choose edit. Fixing the virtual offset (see 3.).
Voila!!!
Everything works.
Posted on 2002-02-18 03:09:02 by masquer
And you actually believe the program will run when you have changed
section RVAs without relinking the instructions and data pointers?
*G*
Posted on 2002-02-18 03:13:36 by f0dder
YES IT IS!!!
Right now I am tested this method with some apps. Everything work. See p.3 and p.9. If you fix the virtual offset.
Posted on 2002-02-18 03:21:25 by masquer
Tks masquer, but it didn?t work, replacing the sections on pe files don?t work with all programs, some of them have fixed data where link to, and a regular peeditor can?t be able to rebuild all of them

f0odder, i tryed to put end start before the _TEXT ENDS, but the problem remains.

The section generated was made by default, inserting .idata and rdata sections where i wanted to be .data.

I?ll check for fasm and see what can be done.

The microsoft reference did not help on this issue. Can you pls, help me with bulding the same stuff with fasm (i?m not familiarized with it yet).

How i insert the included libraries, and separate the sections as i need ? (In fasm)
Posted on 2002-02-18 14:29:54 by Beyond2000!
Masquers approach will not work unless you have relocs in the PE
file and PEeditor can relink... which it wasn't able to last time I checked
it out ;).

All the manual segment definitions... you might get it working, but
I don't think the linker respects it. It might be worth looking into the
GNU ld linker, it has pretty flexible linker scripts - the archive format
is somewhat different though (some hash table differences I think,
because the archive format should *basically* be the same - unix ar format).

fasm? Well, look at the included samples, they got me going.
Posted on 2002-02-18 14:37:11 by f0dder
F0odder

I ment "_TEXT ends" before "end start", that i did too
Posted on 2002-02-18 14:41:16 by Beyond2000!
Well,

the PE has relocs, i tryed, but it didn't work either.

Fasm seems to do the job, (and in fact, it did), i am checking it right now.
Posted on 2002-02-18 16:10:32 by Beyond2000!
f0dder, you were right, Fasm really did relinked section the way needed, it assumes as alignemt of 200, and one problem i figured was the data section virtual size that has mismatched and ended prematurely.

Now i fixed it, increasing some data - it seems to be fixed.

I still geting errors, but this is probably due to the resources hat i need to build in fasm, because the data inside calls some instructions through the resource (like version check etc).

I'll try to finish tomorrow the resource section on fasm.

The main problem on fasm, is that for now it don't have prebuilded libraries. It assembles directly the file, without generating .lib or .obj files. Maybe on the next version it have something of theses like masm.

I still would like to know if masm is able to reorder the sections....it really don't work, no matter what i try
Posted on 2002-02-19 00:52:42 by Beyond2000!
fasm indeed is only suitable for some situations, and I find it messy
to use :). But it does give good flexibility, without having to entirely
handbuild the PE files.

The best solution would probably be to check out mingw32 or cygwin,
read up on the GNU ld linker scripts, and work out something. It's
been a while since I messed with that stuff though, so I can't help
you much there. But the GNU ld linker is pretty damn flexible.
Posted on 2002-02-19 07:10:35 by f0dder