Skip to content

Using patchelf --add-needed and --remove-needed breaks PIE executable #115

Open
@gamazeps

Description

@gamazeps

I currently need patchelf to add a dependency to an elf file (in the jarify project if it's of any use). It seems to fail, here is a minimal repro.

I am building a simple C file compiled with -fPIE/-fpie and calling the following commands

$patchelf --remove-needed libpthread.so.0 thread
$ldd thread
Inconsistency detected by ld.so: dl-version.c: 224: _dl_check_map_versions: Assertion `needed != NULL' failed!
$patchelf --add-needed libpthread.so.0 thread
stat: No such file or director

The Makefile used here is:

CC=gcc
CFLAGS=-fPIE
LDFLAGS=-lpthread -fpie 

OBJECTS=thread.o

hello: $(OBJECTS)
	$(CC) $(CFLAGS) $(OBJECTS) -o thread $(LDFLAGS)

all:hello

.PHONY: clean
clean:
	rm -f *~ *.o thread

The c source file thread.c, (simply the wikipedia example of pthread in order to get a dynamic dependency)

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
 
#define NUM_THREADS     5
 
void* perform_work( void* argument )
{
  int passed_in_value;
 
  passed_in_value = *( ( int* )argument );
  printf( "Hello World! It's me, thread with argument %d!\n", passed_in_value );
 
  /* optionally: insert more useful stuff here */
 
  return NULL;
}
 
int main( int argc, char** argv )
{
  pthread_t threads[ NUM_THREADS ];
  int thread_args[ NUM_THREADS ];
  int result_code;
  unsigned index;
 
  // create all threads one by one
  for( index = 0; index < NUM_THREADS; ++index )
  {
    thread_args[ index ] = index;
    printf("In main: creating thread %d\n", index);
    result_code = pthread_create( &threads[index], NULL, perform_work, &thread_args[index] );
    assert( !result_code );
  }
 
  // wait for each thread to complete
  for( index = 0; index < NUM_THREADS; ++index )
  {
    // block until thread 'index' completes
    result_code = pthread_join( threads[ index ], NULL );
    assert( !result_code );
    printf( "In main: thread %d has completed\n", index );
   }
 
   printf( "In main: All threads completed successfully\n" );
   exit( EXIT_SUCCESS );
}

cc @mboes

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions