1 /* ld.so error exception allocation and deallocation.
2 Copyright (C) 1995-2020 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
28 /* This message we return as a last resort. We define the string in a
29 variable since we have to avoid freeing it and so have to enable
30 a pointer comparison. See below and in dlfcn/dlerror.c. */
31 static const char _dl_out_of_memory
[] = "out of memory";
33 /* Dummy allocation object used if allocating the message buffer
36 oom_exception (struct dl_exception
*exception
)
38 exception
->objname
= "";
39 exception
->errstring
= _dl_out_of_memory
;
40 exception
->message_buffer
= NULL
;
44 __attribute__ ((noreturn
))
45 length_mismatch (void)
47 _dl_fatal_printf ("Fatal error: "
48 "length accounting in _dl_exception_create_format\n");
51 /* Adjust the message buffer to indicate whether it is possible to
52 free it. EXCEPTION->errstring must be a potentially deallocatable
55 adjust_message_buffer (struct dl_exception
*exception
)
57 /* If the main executable is relocated it means the libc's malloc
61 malloced
= (GL(dl_ns
)[LM_ID_BASE
]._ns_loaded
!= NULL
62 && (GL(dl_ns
)[LM_ID_BASE
]._ns_loaded
->l_relocated
!= 0));
65 exception
->message_buffer
= (char *) exception
->errstring
;
67 exception
->message_buffer
= NULL
;
71 _dl_exception_create (struct dl_exception
*exception
, const char *objname
,
72 const char *errstring
)
76 size_t len_objname
= strlen (objname
) + 1;
77 size_t len_errstring
= strlen (errstring
) + 1;
78 char *errstring_copy
= malloc (len_objname
+ len_errstring
);
79 if (errstring_copy
!= NULL
)
81 /* Make a copy of the object file name and the error string. */
82 exception
->objname
= memcpy (__mempcpy (errstring_copy
,
83 errstring
, len_errstring
),
84 objname
, len_objname
);
85 exception
->errstring
= errstring_copy
;
86 adjust_message_buffer (exception
);
89 oom_exception (exception
);
91 rtld_hidden_def (_dl_exception_create
)
94 _dl_exception_create_format (struct dl_exception
*exception
, const char *objname
,
99 size_t len_objname
= strlen (objname
) + 1;
100 /* Compute the length of the result. Include room for two NUL
102 size_t length
= len_objname
+ 1;
106 for (const char *p
= fmt
; *p
!= '\0'; ++p
)
113 length
+= strlen (va_arg (ap
, const char *));
115 /* Recognize the l modifier. It is only important on some
116 platforms where long and int have a different size. We
117 can use the same code for size_t. */
122 length
+= LONG_WIDTH
/ 4;
128 length
+= INT_WIDTH
/ 4;
131 /* Assumed to be '%'. */
141 if (length
> PTRDIFF_MAX
)
143 oom_exception (exception
);
146 char *errstring
= malloc (length
);
147 if (errstring
== NULL
)
149 oom_exception (exception
);
152 exception
->errstring
= errstring
;
153 adjust_message_buffer (exception
);
155 /* Copy the error message to errstring. */
157 /* Next byte to be written in errstring. */
158 char *wptr
= errstring
;
159 /* End of the allocated string. */
160 char *const end
= errstring
+ length
;
165 for (const char *p
= fmt
; *p
!= '\0'; ++p
)
173 const char *ptr
= va_arg (ap
, const char *);
174 size_t len_ptr
= strlen (ptr
);
175 if (len_ptr
> end
- wptr
)
177 wptr
= __mempcpy (wptr
, ptr
, len_ptr
);
188 unsigned long int num
= va_arg (ap
, unsigned int);
190 wptr
+= INT_WIDTH
/ 4;
191 char *cp
= _itoa (num
, wptr
, 16, 0);
192 /* Pad to the full width with 0. */
201 unsigned long int num
= va_arg (ap
, unsigned long int);
203 wptr
+= LONG_WIDTH
/ 4;
204 char *cp
= _itoa (num
, wptr
, 16, 0);
205 /* Pad to the full width with 0. */
213 _dl_fatal_printf ("Fatal error:"
214 " invalid format in exception string\n");
229 if (len_objname
!= end
- wptr
)
231 exception
->objname
= memcpy (wptr
, objname
, len_objname
);
234 rtld_hidden_def (_dl_exception_create_format
)
237 _dl_exception_free (struct dl_exception
*exception
)
239 free (exception
->message_buffer
);
240 exception
->objname
= NULL
;
241 exception
->errstring
= NULL
;
242 exception
->message_buffer
= NULL
;
244 rtld_hidden_def (_dl_exception_free
)