Compatibility rules to follow if we want to modify a function definition in a shared library used by our program

74 views Asked by At

I'm on an Ubuntu x86_64 system.

I have this source file main.c that uses the fx function defined in the shared library libfx.so :

int fx( int a, int b);

int main( void ){
     
    int x = fx( 50, 30 );
    return 0;

}

Here is the definition of fx in the file fx.c used to create the shared library :

int fx( int a, int b){

    return a + b;

}

Questions :

  1. Is it correct to say that we can modify the function definition internally as we like without causing incompatibilities ? For example we could do :
int fx( int a, int b){

    return a - b;
}

or

int fx( int a, int b){

    if ( a > 10 ){
        
         return a + b;

    }else{
         
         return a - b;

    }
}
  1. Is it correct to say that incompatibilities occur if we modify any of the following :

    return type

    function name

    numbers of paramters

    type of paramaters

  2. Do these principles also apply to single object files ? For example an object file called A that uses a function defined in B

1

There are 1 answers

5
Eric Postpischil On
  1. Is it correct to say that we can modify the function definition internally as we like without causing incompatibilities ?

Not entirely. It is correct that modifying the function definition will not cause any problems in passing arguments or receiving return values. However, changing the behavior of a function may change the resulting behavior of the calling function.

The questions you ask about appear to be in a specific limited context and may only be asking about compatibility of the calling interface specifically and not intended to include behavior compatibility.

  1. Is it correct to say that incompatibilities occur if we modify any of the following :

return type

Changing the return type often causes interface incompatibility. There are some limited circumstances in which it does not, such as changing to a compatible type.

function name

Changing the function name often causes interface incompatibility. There are some limited circumstances in which it does not, such as changing the trailing characters of a name longer than the significant portion for external names. (The C standard requires at least 31 significant characters.)

numbers of paramters

Changing the number of parameters often causes interface incompatibility. There are some limited circumstances in which it does not, such as reducing the number of parameters in platforms in which the calling routine is responsible for stack management related to passing parameters. (In other words, in some platforms, the calling routine may pass extra parameters which will be ignored by the called routine.)

type of paramaters

Changing the type of parameters often causes interface incompatibility. There are some limited circumstances in which it does not, such as changing to a compatible type.

Do these principles also apply to single object files ? For example an object file called A that uses a function defined in B

Yes.