RE: C++ functions, init and paged code...

The short answer is you do not want to do this. If you are using C++
you cannot be sure where code the compiler generates such as
constructors and destructors will be placed so using any page able
sections is a bad idea. This is one of the down sides of using C++ in
the kernel, you are wasting non-paged memory.

Now that I have warned you off, the actual effort is:

#pragma alloc_text( INIT, DriverEntry )

Where INIT could be replaced by PAGED and DriverEntry by any function
name.

Don Burn (MVP, Windows DKD)
Windows Filesystem and Driver Consulting
Website: http://www.windrvr.com
Blog: http://msmvps.com/blogs/WinDrvr

-----Original Message-----
From: Robert Randall [mailto:xxxxx@gmail.com]
Posted At: Tuesday, June 29, 2010 10:45 AM
Posted To: ntfsd
Conversation: [ntdev] C++ functions, init and paged code…
Subject: [ntdev] C++ functions, init and paged code…

I’m sure this question has been answered many times but my search
engine
skills appear to be a bit less than adequate…

How do I get the linker to put a C++ function into the INIT section or
the
PAGED code section?

Thanks,
Robert.


Robert Randall | xxxxx@gmail.com

__________ Information from ESET Smart Security, version of virus
signature
database 5237 (20100629) __________

The message was checked by ESET Smart Security.

http://www.eset.com

Thanks Don. That works fine because DriverEntry MUST be extern ‘C’.
I’m really writing C++ code (no classes yet). I’m compiling functions
using C++. So without the complexity of constructors/destructors,
custom allocators, etc. My guess is there is a simply way to ask the
linker to put the C++ function into the correct segment. Or is it
much more complicated than that? It has been a while (too long to
admin) since my compiler courses at UofM…

Thanks.
-Robert.

On Tue, Jun 29, 2010 at 9:53 AM, Don Burn wrote:
> The short answer is you do not want to do this. ?If you are using C++
> you cannot be sure where code the compiler generates such as
> constructors and destructors will be placed so using any page able
> sections is a bad idea. ?This is one of the down sides of using C++ in
> the kernel, you are wasting non-paged memory.
>
> Now that I have warned you off, the actual effort is:
>
> ? ?#pragma alloc_text( INIT, DriverEntry )
>
> Where INIT could be replaced by PAGED and DriverEntry by any function
> name.
>
>
> Don Burn (MVP, Windows DKD)
> Windows Filesystem and Driver Consulting
> Website: http://www.windrvr.com
> Blog: http://msmvps.com/blogs/WinDrvr
>
>
>
>
>> -----Original Message-----
>> From: Robert Randall [mailto:xxxxx@gmail.com]
>> Posted At: Tuesday, June 29, 2010 10:45 AM
>> Posted To: ntfsd
>> Conversation: [ntdev] C++ functions, init and paged code…
>> Subject: [ntdev] C++ functions, init and paged code…
>>
>> I’m sure this question has been answered many times but my search
> engine
>> skills appear to be a bit less than adequate…
>>
>> How do I get the linker to put a C++ function into the INIT section or
> the
>> PAGED code section?
>>
>> Thanks,
>> Robert.
>>
>> –
>> Robert Randall | xxxxx@gmail.com
>>
>>
>> Information from ESET Smart Security, version of virus
> signature
>> database 5237 (20100629)

>>
>> The message was checked by ESET Smart Security.
>>
>> http://www.eset.com
>>
>
>
> —
> NTFSD is sponsored by OSR
>
> For our schedule of debugging and file system seminars
> (including our new fs mini-filter seminar) visit:
> http://www.osr.com/seminars
>
> To unsubscribe, visit the List Server section of OSR Online at http://www.osronline.com/page.cfm?name=ListServer
>


Robert Randall | xxxxx@gmail.com

This only works with C++ functions defined as extern “C”.

Bill Wandel

-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com] On Behalf Of Don Burn
Sent: Tuesday, June 29, 2010 10:53 AM
To: Windows File Systems Devs Interest List
Subject: RE:[ntfsd] C++ functions, init and paged code…

The short answer is you do not want to do this. If you are using C++ you
cannot be sure where code the compiler generates such as constructors and
destructors will be placed so using any page able sections is a bad idea.
This is one of the down sides of using C++ in the kernel, you are wasting
non-paged memory.

Now that I have warned you off, the actual effort is:

#pragma alloc_text( INIT, DriverEntry )

Where INIT could be replaced by PAGED and DriverEntry by any function name.

Don Burn (MVP, Windows DKD)
Windows Filesystem and Driver Consulting
Website: http://www.windrvr.com
Blog: http://msmvps.com/blogs/WinDrvr

-----Original Message-----
From: Robert Randall [mailto:xxxxx@gmail.com]
Posted At: Tuesday, June 29, 2010 10:45 AM Posted To: ntfsd
Conversation: [ntdev] C++ functions, init and paged code…
Subject: [ntdev] C++ functions, init and paged code…

I’m sure this question has been answered many times but my search
engine
skills appear to be a bit less than adequate…

How do I get the linker to put a C++ function into the INIT section or
the
PAGED code section?

Thanks,
Robert.


Robert Randall | xxxxx@gmail.com

__________ Information from ESET Smart Security, version of virus
signature
database 5237 (20100629) __________

The message was checked by ESET Smart Security.

http://www.eset.com


NTFSD is sponsored by OSR

For our schedule of debugging and file system seminars (including our new fs
mini-filter seminar) visit:
http://www.osr.com/seminars

To unsubscribe, visit the List Server section of OSR Online at
http://www.osronline.com/page.cfm?name=ListServer

You typically just do it per function. Also you want to have the
PAGED_CODE macro in the function, since PreFast will complain if it is
paged and that is not present. It is a good thing to do anyway since
the macro tests in case you accidently call the function at DISPATCH or
higher.

For most callbacks these days the WDK headers define a function type as
well as a function pointer type. So I take advantage of that, my
typical function header looks like:

static EVT_WDF_DEVICE_FILE_CREATE EvtFileCreate;
#pragma alloc_text (PAGE, EvtFileCreate)
static VOID EvtFileCreate ( IN WDFDEVICE Device,
IN WDFREQUEST Request,
IN WDFFILEOBJECT FileObject )
{

}

Don Burn (MVP, Windows DKD)
Windows Filesystem and Driver Consulting
Website: http://www.windrvr.com
Blog: http://msmvps.com/blogs/WinDrvr

-----Original Message-----
From: Robert Randall [mailto:xxxxx@gmail.com]
Posted At: Tuesday, June 29, 2010 11:00 AM
Posted To: ntfsd
Conversation: C++ functions, init and paged code…
Subject: Re: C++ functions, init and paged code…

Thanks Don. That works fine because DriverEntry MUST be extern ‘C’.
I’m really writing C++ code (no classes yet). I’m compiling functions
using
C++. So without the complexity of constructors/destructors, custom
allocators, etc. My guess is there is a simply way to ask the linker
to put
the C++ function into the correct segment. Or is it
much more complicated than that? It has been a while (too long to
admin) since my compiler courses at UofM…

Thanks.
-Robert.

On Tue, Jun 29, 2010 at 9:53 AM, Don Burn wrote:
> > The short answer is you do not want to do this. If you are using
C++
> > you cannot be sure where code the compiler generates such as
> > constructors and destructors will be placed so using any page able
> > sections is a bad idea. This is one of the down sides of using C++
in
> > the kernel, you are wasting non-paged memory.
> >
> > Now that I have warned you off, the actual effort is:
> >
> > #pragma alloc_text( INIT, DriverEntry )
> >
> > Where INIT could be replaced by PAGED and DriverEntry by any
function
> > name.
> >
> >
> > Don Burn (MVP, Windows DKD)
> > Windows Filesystem and Driver Consulting
> > Website: http://www.windrvr.com
> > Blog: http://msmvps.com/blogs/WinDrvr
> >
> >
> >
> >
> >> -----Original Message-----
> >> From: Robert Randall [mailto:xxxxx@gmail.com]
> >> Posted At: Tuesday, June 29, 2010 10:45 AM Posted To: ntfsd
> >> Conversation: [ntdev] C++ functions, init and paged code…
> >> Subject: [ntdev] C++ functions, init and paged code…
> >>
> >> I’m sure this question has been answered many times but my search
> > engine
> >> skills appear to be a bit less than adequate…
> >>
> >> How do I get the linker to put a C++ function into the INIT section
> >> or
> > the
> >> PAGED code section?
> >>
> >> Thanks,
> >> Robert.
> >>
> >> –
> >> Robert Randall | xxxxx@gmail.com
> >>
> >>
> >> Information from ESET Smart Security, version of virus
> > signature
> >> database 5237 (20100629)

> >>
> >> The message was checked by ESET Smart Security.
> >>
> >> http://www.eset.com
> >>
> >
> >
> > —
> > NTFSD is sponsored by OSR
> >
> > For our schedule of debugging and file system seminars (including
our
> > new fs mini-filter seminar) visit:
> > http://www.osr.com/seminars
> >
> > To unsubscribe, visit the List Server section of OSR Online at
> > http://www.osronline.com/page.cfm?name=ListServer
> >
>
>
>
> –
> Robert Randall | xxxxx@gmail.com
>
>
> Information from ESET Smart Security, version of virus
signature
> database 5237 (20100629)

>
> The message was checked by ESET Smart Security.
>
> http://www.eset.com
>

Hmm… Well, a C++ function defined as extern “C” is not a C++
function, it is a C function. So this doesn’t really help much. I’ll
figure it out and post an answer.

-Robert.

On Tue, Jun 29, 2010 at 10:05 AM, Bill Wandel wrote:
> This only works with C++ functions defined as extern “C”.
>
> Bill Wandel
>
> -----Original Message-----
> From: xxxxx@lists.osr.com
> [mailto:xxxxx@lists.osr.com] On Behalf Of Don Burn
> Sent: Tuesday, June 29, 2010 10:53 AM
> To: Windows File Systems Devs Interest List
> Subject: RE:[ntfsd] C++ functions, init and paged code…
>
> The short answer is you do not want to do this. ?If you are using C++ you
> cannot be sure where code the compiler generates such as constructors and
> destructors will be placed so using any page able sections is a bad idea.
> This is one of the down sides of using C++ in the kernel, you are wasting
> non-paged memory.
>
> Now that I have warned you off, the actual effort is:
>
> ? ?#pragma alloc_text( INIT, DriverEntry )
>
> Where INIT could be replaced by PAGED and DriverEntry by any function name.
>
>
> Don Burn (MVP, Windows DKD)
> Windows Filesystem and Driver Consulting
> Website: http://www.windrvr.com
> Blog: http://msmvps.com/blogs/WinDrvr
>
>
>
>
>> -----Original Message-----
>> From: Robert Randall [mailto:xxxxx@gmail.com]
>> Posted At: Tuesday, June 29, 2010 10:45 AM Posted To: ntfsd
>> Conversation: [ntdev] C++ functions, init and paged code…
>> Subject: [ntdev] C++ functions, init and paged code…
>>
>> I’m sure this question has been answered many times but my search
> engine
>> skills appear to be a bit less than adequate…
>>
>> How do I get the linker to put a C++ function into the INIT section or
> the
>> PAGED code section?
>>
>> Thanks,
>> Robert.
>>
>> –
>> Robert Randall | xxxxx@gmail.com
>>
>>
>> Information from ESET Smart Security, version of virus
> signature
>> database 5237 (20100629)

>>
>> The message was checked by ESET Smart Security.
>>
>> http://www.eset.com
>>
>
>
> —
> NTFSD is sponsored by OSR
>
> For our schedule of debugging and file system seminars (including our new fs
> mini-filter seminar) visit:
> http://www.osr.com/seminars
>
> To unsubscribe, visit the List Server section of OSR Online at
> http://www.osronline.com/page.cfm?name=ListServer
>
>
> —
> NTFSD is sponsored by OSR
>
> For our schedule of debugging and file system seminars
> (including our new fs mini-filter seminar) visit:
> http://www.osr.com/seminars
>
> To unsubscribe, visit the List Server section of OSR Online at http://www.osronline.com/page.cfm?name=ListServer
>


Robert Randall | xxxxx@gmail.com

No that is the answer and yes it is a C++ function as you cannot compile
that expression in C. The mandated name demangling is the trick here that
allows the page specification pragma to work, and by implication, why it
doesn’t work for non demangled C++ function names.

Mark Roddy

On Tue, Jun 29, 2010 at 12:30 PM, Robert Randall > wrote:

> Hmm… Well, a C++ function defined as extern “C” is not a C++
> function, it is a C function. So this doesn’t really help much. I’ll
> figure it out and post an answer.
>
> -Robert.
>
> On Tue, Jun 29, 2010 at 10:05 AM, Bill Wandel
> wrote:
> > This only works with C++ functions defined as extern “C”.
> >
> > Bill Wandel
> >
> > -----Original Message-----
> > From: xxxxx@lists.osr.com
> > [mailto:xxxxx@lists.osr.com] On Behalf Of Don Burn
> > Sent: Tuesday, June 29, 2010 10:53 AM
> > To: Windows File Systems Devs Interest List
> > Subject: RE:[ntfsd] C++ functions, init and paged code…
> >
> > The short answer is you do not want to do this. If you are using C++ you
> > cannot be sure where code the compiler generates such as constructors and
> > destructors will be placed so using any page able sections is a bad idea.
> > This is one of the down sides of using C++ in the kernel, you are wasting
> > non-paged memory.
> >
> > Now that I have warned you off, the actual effort is:
> >
> > #pragma alloc_text( INIT, DriverEntry )
> >
> > Where INIT could be replaced by PAGED and DriverEntry by any function
> name.
> >
> >
> > Don Burn (MVP, Windows DKD)
> > Windows Filesystem and Driver Consulting
> > Website: http://www.windrvr.com
> > Blog: http://msmvps.com/blogs/WinDrvr
> >
> >
> >
> >
> >> -----Original Message-----
> >> From: Robert Randall [mailto:xxxxx@gmail.com]
> >> Posted At: Tuesday, June 29, 2010 10:45 AM Posted To: ntfsd
> >> Conversation: [ntdev] C++ functions, init and paged code…
> >> Subject: [ntdev] C++ functions, init and paged code…
> >>
> >> I’m sure this question has been answered many times but my search
> > engine
> >> skills appear to be a bit less than adequate…
> >>
> >> How do I get the linker to put a C++ function into the INIT section or
> > the
> >> PAGED code section?
> >>
> >> Thanks,
> >> Robert.
> >>
> >> –
> >> Robert Randall | xxxxx@gmail.com
> >>
> >>
> >> Information from ESET Smart Security, version of virus
> > signature
> >> database 5237 (20100629)

> >>
> >> The message was checked by ESET Smart Security.
> >>
> >> http://www.eset.com
> >>
> >
> >
> > —
> > NTFSD is sponsored by OSR
> >
> > For our schedule of debugging and file system seminars (including our new
> fs
> > mini-filter seminar) visit:
> > http://www.osr.com/seminars
> >
> > To unsubscribe, visit the List Server section of OSR Online at
> > http://www.osronline.com/page.cfm?name=ListServer
> >
> >
> > —
> > NTFSD is sponsored by OSR
> >
> > For our schedule of debugging and file system seminars
> > (including our new fs mini-filter seminar) visit:
> > http://www.osr.com/seminars
> >
> > To unsubscribe, visit the List Server section of OSR Online at
> http://www.osronline.com/page.cfm?name=ListServer
> >
>
>
>
> –
> Robert Randall | xxxxx@gmail.com
>
> —
> NTFSD is sponsored by OSR
>
> For our schedule of debugging and file system seminars
> (including our new fs mini-filter seminar) visit:
> http://www.osr.com/seminars
>
> To unsubscribe, visit the List Server section of OSR Online at
> http://www.osronline.com/page.cfm?name=ListServer
>

What I do is this… I group all of the paged functions together and all of the non-paged functions together in each source file. Then I precede each of the groups of functions with one of the following:

#pragma code_seg() // non-paged functions start here
#pragma code_seg(“PAGE”) // paged functions start here
#pragma code_seg(“INIT”) // discarded functions start here

Bruce Engle

This is what I did many years ago with a NT4 driver. This was before
Microsoft had the C++ restrictions document. You don’t have complete control
of which sections the compiler puts some of the code in. It is possible for
code that should be non paged to end up in a paged section. One example of
this is two classes with the same signature. Only one will generate code. If
that one is in a pages section you have a problem.

Bill Wandel

-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com] On Behalf Of xxxxx@serena.com
Sent: Saturday, July 03, 2010 11:26 AM
To: Windows File Systems Devs Interest List
Subject: RE:[ntfsd] RE: C++ functions, init and paged code…

What I do is this… I group all of the paged functions together and all of
the non-paged functions together in each source file. Then I precede each
of the groups of functions with one of the following:

#pragma code_seg() // non-paged functions start here
#pragma code_seg(“PAGE”) // paged functions start here
#pragma code_seg(“INIT”) // discarded functions start here

Bruce Engle


NTFSD is sponsored by OSR

For our schedule of debugging and file system seminars (including our new fs
mini-filter seminar) visit:
http://www.osr.com/seminars

To unsubscribe, visit the List Server section of OSR Online at
http://www.osronline.com/page.cfm?name=ListServer

It is not a restrictions document it is a document warning of the
problems. There are actually more potential problems than that
document, but it is a good start.

Don Burn (MVP, Windows DKD)
Windows Filesystem and Driver Consulting
Website: http://www.windrvr.com
Blog: http://msmvps.com/blogs/WinDrvr

-----Original Message-----
From: Bill Wandel [mailto:xxxxx@bwandel.com]
Posted At: Saturday, July 03, 2010 4:22 PM
Posted To: ntfsd
Conversation: RE: C++ functions, init and paged code…
Subject: RE: RE: C++ functions, init and paged code…

This is what I did many years ago with a NT4 driver. This was before
Microsoft
had the C++ restrictions document. You don’t have complete control of
which
sections the compiler puts some of the code in. It is possible for
code that
should be non paged to end up in a paged section. One example of this
is two
classes with the same signature. Only one will generate code. If that
one is
in a pages section you have a problem.

Bill Wandel

-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com] On Behalf Of
xxxxx@serena.com
Sent: Saturday, July 03, 2010 11:26 AM
To: Windows File Systems Devs Interest List
Subject: RE:[ntfsd] RE: C++ functions, init and paged code…

What I do is this… I group all of the paged functions together and
all of
the non-paged functions together in each source file. Then I precede
each of
the groups of functions with one of the following:

#pragma code_seg() // non-paged functions start here
#pragma code_seg(“PAGE”) // paged functions start here
#pragma code_seg(“INIT”) // discarded functions start here

Bruce Engle


NTFSD is sponsored by OSR

For our schedule of debugging and file system seminars (including our
new fs
mini-filter seminar) visit:
http://www.osr.com/seminars

To unsubscribe, visit the List Server section of OSR Online at
http://www.osronline.com/page.cfm?name=ListServer

__________ Information from ESET Smart Security, version of virus
signature
database 5248 (20100703) __________

The message was checked by ESET Smart Security.

http://www.eset.com

I try to avoid the sort of problems described in that Microsoft document by not using any C++ features that result in compiler-generated code. There are only POD structures and no classes. I am basically using C++ only for language syntax improvements and better type checking.

Bruce Engle

Thanks All! I am doing the same as Bruce. I would like to experiment
more with using C++ language features in drivers but I’m not eager to
make a customer pay for my learning curve. I intend to try to push
the boundaries with a simple device filter when I have the time.

Best,
Robert.

On Sun, Jul 4, 2010 at 12:56 AM, wrote:
> I try to avoid the sort of problems described in that Microsoft document by not using any C++ features that result in compiler-generated code. ?There are only POD structures and no classes. ?I am basically using C++ only for language syntax improvements and better type checking.
>
> Bruce Engle
>
> —
> NTFSD is sponsored by OSR
>
> For our schedule of debugging and file system seminars
> (including our new fs mini-filter seminar) visit:
> http://www.osr.com/seminars
>
> To unsubscribe, visit the List Server section of OSR Online at http://www.osronline.com/page.cfm?name=ListServer
>


Robert Randall | xxxxx@gmail.com