Windows System Software -- Consulting, Training, Development -- Unique Expertise, Guaranteed Results

Before Posting...
Please check out the Community Guidelines in the Announcements and Administration Category.

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

Don_Burn_1Don_Burn_1 Member Posts: 4,311
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
>

Comments

  • Robert_RandallRobert_Randall Member Posts: 100
    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 <xxxxx@acm.org> 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
  • OSR_Community_UserOSR_Community_User Member Posts: 110,217
    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
  • Don_Burn_1Don_Burn_1 Member Posts: 4,311
    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 <xxxxx@acm.org> 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
    >
  • Robert_RandallRobert_Randall Member Posts: 100
    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 <xxxxx@bwandel.com> 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
  • Mark_RoddyMark_Roddy Member - All Emails Posts: 4,306
    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
    >
  • Bruce_EngleBruce_Engle Member Posts: 13
    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
  • OSR_Community_UserOSR_Community_User Member Posts: 110,217
    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
  • Don_Burn_1Don_Burn_1 Member Posts: 4,311
    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
    >
  • Bruce_EngleBruce_Engle Member Posts: 13
    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
  • Robert_RandallRobert_Randall Member Posts: 100
    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, <xxxxx@serena.com> 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
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Upcoming OSR Seminars
Developing Minifilters 29 July 2019 OSR Seminar Space
Writing WDF Drivers 23 Sept 2019 OSR Seminar Space
Kernel Debugging 21 Oct 2019 OSR Seminar Space
Internals & Software Drivers 18 Nov 2019 Dulles, VA