position independet code

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • junky_fellow

    #1

    position independet code

    What is a position independent code ? What are the basic guidelines to
    keep in mind while writing a position independent code ?

    thanx for any help ....
  • jacob navia

    #2
    Re: position independet code

    junky_fellow wrote:[color=blue]
    > What is a position independent code ? What are the basic guidelines to
    > keep in mind while writing a position independent code ?
    >
    > thanx for any help ....[/color]
    Position independent code is assembly that can be loaded at any address
    for execution.
    There is no way you can do that in C, since the objective of writing in
    C is to avoid writing in assembly language. Some compilers support this,
    and there are compile time flags to direct the compiler to generate that
    kind of code.

    Comment

    • Malcolm

      #3
      Re: position independet code


      "junky_fell ow" <junky_fellow@y ahoo.co.in> wrote in message[color=blue]
      > What is a position independent code ? What are the basic guidelines to
      > keep in mind while writing a position independent code ?
      >[/color]
      Imagine this (pseudo assembly)

      // increments accumulator if not equal to X register

      8000 CMP A, X // compare accumulator to x register
      8001 JMPZ 8003 // jump if equal to 8003
      8002 INC A // increment A
      8003 RET // returns

      Ad this

      8000 CMP A, X
      8001 BRNZ +2 // BRNZ branches forwards or backwards by a set number
      8002 INC A
      8003 RET

      The second will work if moved to a new position, but the first has the jump
      destination hardcoded and will not.


      Comment

      • xarax

        #4
        Re: position independet code

        "jacob navia" <jacob@jacob.re mcomp.fr> wrote in message
        news:41835032$0 $3590$8fcfb975@ news.wanadoo.fr ...[color=blue]
        > junky_fellow wrote:[color=green]
        > > What is a position independent code ? What are the basic guidelines to
        > > keep in mind while writing a position independent code ?
        > >
        > > thanx for any help ....[/color]
        > Position independent code is assembly that can be loaded at any address
        > for execution.[/color]

        It's more than that. Position independent code can be
        loaded at any address and later it can be moved/copied
        (in its entirety) to another address, and it will run
        correctly from that new address.

        Most compile/link procedures will generate internal
        constants in the object code that are called "relocatabl e
        address constants" (RAC). When a program is loaded into
        memory for execution, the loader will "fix up" all of
        the RAC fields by adding the base address of the program
        to each RAC field, thus yielding a "position dependent"
        address constant field that the program can use.

        If such a program were copied to another memory location,
        the RAC fields would have the old address values and the
        program would misbehave in strange and usually disastrous
        ways.

        A position independent program does not have RAC fields.
        Thus, it can be loaded at one memory address and later
        copied to another memory address, and it will still run
        at the new address. All memory references in the program
        are calculated at run-time according to its current
        location in memory, rather than loading RAC fields.
        [color=blue]
        > There is no way you can do that in C, since the objective of writing in
        > C is to avoid writing in assembly language. Some compilers support this,
        > and there are compile time flags to direct the compiler to generate that
        > kind of code.[/color]

        There is nothing in the C language that pertains to
        position dependent/independent programs, or to
        programs that can loaded anywhere (also called
        "relocatabl e") or must be loaded at a specific memory
        address (usually device drivers must live at a specific
        memory address and refer to specific memory locations
        which outside the C language).


        --
        ----------------------------
        Jeffrey D. Smith
        Farsight Systems Corporation
        24 BURLINGTON DRIVE
        LONGMONT, CO 80501-6906

        z/Debug debugs your Systems/C programs running on IBM z/OS for FREE!



        Comment

        • jacob navia

          #5
          Re: position independet code

          xarax wrote:[color=blue]
          >
          > It's more than that. Position independent code can be
          > loaded at any address and later it can be moved/copied
          > (in its entirety) to another address, and it will run
          > correctly from that new address.
          >[/color]

          Yes, you are right, my wording is misleading.
          Position independent code can be relocated an undefinite number of
          times.

          Thanks for this clarification.

          Comment

          • Chris Torek

            #6
            Re: position independet code

            >>junky_fello w wrote:[color=blue][color=green][color=darkred]
            >>> What is a position independent code ? What are the basic guidelines to
            >>> keep in mind while writing a position independent code ?[/color][/color][/color]

            I was not going to say anything, because (as xarax notes) Standard
            C insulates one entirely from the position-independence issues:
            you write your code in Standard C and it "just works". If you need
            the stuff that PIC may provide, you will not be writing Standard
            C, and any non-standard compiler flags (such as -fpic) and/or
            C-callable functions (such as mmap()) that you may need are, well,
            non-standard -- you will have to investigate the details elsewhere.
            [color=blue]
            >"jacob navia" <jacob@jacob.re mcomp.fr> wrote in message
            >news:41835032$ 0$3590$8fcfb975 @news.wanadoo.f r...[color=green]
            >> Position independent code is assembly that can be loaded at any address
            >> for execution.[/color][/color]

            In article <kONgd.12868$KJ 6.5102@newsread 1.news.pas.eart hlink.net>,
            xarax <xarax@email.co m> wrote:[color=blue]
            >It's more than that. Position independent code can be
            >loaded at any address and later it can be moved/copied
            >(in its entirety) to another address, and it will run
            >correctly from that new address.[/color]

            PIC *used* to mean that, back when I wrote such things by hand.

            These days the term has been taken over, at least on Unix-like
            systems, to refer to code that is relocatable but not actually
            "position independent", even though it will be run inside a
            virtual-memory process, so that a dynamic loader can load new code
            at run-time. GCC's "-fpic" and/or "-fPIC" flags (which may or may
            not have subtly different meanings, depending on processor architecture
            -- typically the uppercase version gives you larger displacements,
            if "small+fast " and "large+slow " are both offered on the architecture)
            tell it to build dynamic-relocation tables for this sort of
            not-really-position-independent-code.

            In other words, the meaning of "PIC" is now at best muddy: not only
            is it off-topic in comp.lang.c, but different people will have
            different ideas of what it *should* mean, on different systems. :-)
            --
            In-Real-Life: Chris Torek, Wind River Systems
            Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
            email: forget about it http://web.torek.net/torek/index.html
            Reading email is like searching for food in the garbage, thanks to spammers.

            Comment

            Working...