diff --git a/src/ln.c b/src/ln.c
index 1aa1473..13537ac 100644
--- a/src/ln.c
+++ b/src/ln.c
@@ -132,21 +132,23 @@ target_directory_operand (char const *file)
 static char *
 convert_abs_rel (const char *from, const char *target)
 {
-  char *realtarget = canonicalize_filename_mode (target, CAN_MISSING);
+  /* Get dirname to generate paths relative to
+     and to not resolve the last component of target.  */
+  char *targetdir = dir_name (target);
+
+  char *realtarget = canonicalize_filename_mode (targetdir, CAN_MISSING);
   char *realfrom = canonicalize_filename_mode (from, CAN_MISSING);
 
   /* Write to a PATH_MAX buffer.  */
   char *relative_from = xmalloc (PATH_MAX);
 
-  /* Get dirname to generate paths relative to.  */
-  realtarget[dir_len (realtarget)] = '\0';
-
   if (!relpath (realfrom, realtarget, relative_from, PATH_MAX))
     {
       free (relative_from);
       relative_from = NULL;
     }
 
+  free (targetdir);
   free (realtarget);
   free (realfrom);
 
