# Copyright 2002
# Free Software Foundation, Inc.

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  

# Please email any bugs, comments, and/or additions to this file to:
# bug-gdb@prep.ai.mit.edu

# This file was written by Jason Molenda (jmolenda@apple.com)

if $tracelevel then {
        strace $tracelevel
}


# Test an executable with a weak library.  A "weak library" is unlike any
# ELF thing; if all externally visible functions in a shared library are
# marked as "weak" in their prototypes in a C file, then when that executable
# is run, the shared library may be absent and the program will still work.
# (as oppposed to the normal case, where you'd get an unresolved shared library
# reference from the dynamic linker run-time).  All the addresses of the
# functions from the shared library will have a value of 0 in this case.

set timeout 30

set prms_id 0
set bug_id 0

set testfile "weak-prog"
set binfile ${objdir}/${subdir}/${testfile}
set srcfile ${srcdir}/${subdir}/${testfile}.c

set libfile "libweak"
set libbinfile ${objdir}/${subdir}/${libfile}.dylib
set libsrcfile ${srcdir}/${subdir}/${libfile}.c

set additional_flags "additional_flags=-dynamiclib"
if  { [gdb_compile "${libsrcfile}" "${libbinfile}" executable [list debug $additional_flags]] != "" } {
     gdb_suppress_entire_file "Testcase compile failed, so all tests in this file will automatically fail."
}

global env

set ::env(MACOSX_DEPLOYMENT_TARGET) "10.2"
set additional_flags "additional_flags=-L${objdir}/${subdir} -lweak"
if  { [gdb_compile "${srcfile}" "${binfile}" executable [list debug $additional_flags]] != "" } {
     gdb_suppress_entire_file "Testcase compile failed, so all tests in this file will automatically fail."
}

# Start with a fresh gdb

gdb_exit

gdb_start
gdb_reinitialize_dir $srcdir/$subdir
gdb_test "file ${binfile}" "Reading symbols for shared libraries (\\.)+ done\[\r\n\]+Reading symbols from ${binfile}(\\.)+done." "load with weak lib, should be no warnings"

gdb_test "future-break foo" "Function \"foo\" not defined..*Breakpoint 1 at 0x0.*" "Set breakpoint on foo, not yet defined"

gdb_test "run" "Starting program: $binfile *.*\\\[Switching to process $decimal thread $hex\].*Reading symbols for shared libraries \\.+ done.*Re-enabling shared library breakpoints: 1.*hi, in main.*Breakpoint 1, foo.*libweak.c.*" "continue to weak library bp"

gdb_test "info sharedlibrary" "$decimal libweak.dylib *- $hex *dyld Y Y \"$libbinfile\" at $hex \\(offset $hex\\).*"
gdb_exit



# Now move the library aside, try again

remote_exec build "mv ${libbinfile} ${libbinfile}.hide"

gdb_start
gdb_reinitialize_dir $srcdir/$subdir
gdb_test "file ${binfile}" "Reading symbols from ${binfile}\\.+done.\[\r\n\]+Reading symbols for shared libraries \\.\\. done" "load with missing weak lib, hope for no warnings"

gdb_test "info sharedlibrary" ".*$decimal libweak.dylib *- - *init N N \"${libbinfile}\".*" "check sharedlibrary entry for missing weak library pre-run"

gdb_test "future-break foo" "Function \"foo\" not defined..*Breakpoint 1 at 0x0.*" "Set breakpoint on foo, not yet defined"

gdb_test "run" "Starting program: $binfile \[\r\n\]+\\\[Switching to process $decimal thread $hex\\\]\[\r\n\]+(Reading symbols for shared libraries (\\.)+ done\[\r\n\]+)+hi, in main\[\r\n\]+finished in main\[\r\n\]+Program exited with code $decimal\\.\[\r\n\]+"  "runtest"

gdb_test "info sharedlibrary" ".*$decimal libweak.dylib *- - *cached-lib N N \"${libbinfile}\".*" "check sharedlibrary entry for missing weak library post-run"
gdb_exit



return 0
